import type { QueryInterface } from 'sequelize';
import { DataTypes } from 'sequelize';

module.exports = {
  up: async (queryInterface: QueryInterface) => {
    // 1) Create read-log table for processed SAP CSV files
    await queryInterface.createTable('from16_sap_read_file', {
      id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
      file_name: { type: DataTypes.STRING(255), allowNull: false, unique: true },
      total_records: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0 },
      total_credit_notes: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0 },
      total_debit_notes: { type: DataTypes.INTEGER, allowNull: false, defaultValue: 0 },
      created_at: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW },
      updated_at: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW },
    }).catch(() => {});

    // 2) Add required new fields to form16_sap_responses
    await queryInterface.addColumn('form16_sap_responses', 'doc_no', {
      type: DataTypes.STRING(200),
      allowNull: true,
    }).catch(() => {});

    // 3) Drop old fields from form16_sap_responses (as requested)
    for (const col of [
      'type',
      'file_name',
      'credit_note_id',
      'debit_note_id',
      'claim_number',
      'sap_document_number',
      'doc_date',
      'tds_amt',
      'raw_row',
      'storage_url',
    ]) {
      await queryInterface.removeColumn('form16_sap_responses', col).catch(() => {});
    }

    // 4) Ensure required columns exist for new contract
    await queryInterface.addColumn('form16_sap_responses', 'trns_uniq_no', {
      type: DataTypes.STRING(200),
      allowNull: true,
    }).catch(() => {});

    await queryInterface.addColumn('form16_sap_responses', 'tds_trns_id', {
      type: DataTypes.STRING(200),
      allowNull: true,
    }).catch(() => {});

    await queryInterface.addColumn('form16_sap_responses', 'msg_typ', {
      type: DataTypes.STRING(20),
      allowNull: true,
    }).catch(() => {});

    await queryInterface.addColumn('form16_sap_responses', 'message', {
      type: DataTypes.TEXT,
      allowNull: true,
    }).catch(() => {});

    await queryInterface.addIndex('form16_sap_responses', ['tds_trns_id'], {
      name: 'idx_form16_sap_responses_tds_trns_id',
    }).catch(() => {});
    await queryInterface.addIndex('form16_sap_responses', ['trns_uniq_no'], {
      name: 'idx_form16_sap_responses_trns_uniq_no',
    }).catch(() => {});
    await queryInterface.addIndex('from16_sap_read_file', ['file_name'], {
      name: 'idx_from16_sap_read_file_name',
    }).catch(() => {});
  },

  down: async (queryInterface: QueryInterface) => {
    // Recreate old columns in form16_sap_responses
    await queryInterface.addColumn('form16_sap_responses', 'type', { type: DataTypes.STRING(10), allowNull: false, defaultValue: 'credit' }).catch(() => {});
    await queryInterface.addColumn('form16_sap_responses', 'file_name', { type: DataTypes.STRING(255), allowNull: true }).catch(() => {});
    await queryInterface.addColumn('form16_sap_responses', 'credit_note_id', { type: DataTypes.INTEGER, allowNull: true }).catch(() => {});
    await queryInterface.addColumn('form16_sap_responses', 'debit_note_id', { type: DataTypes.INTEGER, allowNull: true }).catch(() => {});
    await queryInterface.addColumn('form16_sap_responses', 'claim_number', { type: DataTypes.STRING(100), allowNull: true }).catch(() => {});
    await queryInterface.addColumn('form16_sap_responses', 'sap_document_number', { type: DataTypes.STRING(100), allowNull: true }).catch(() => {});
    await queryInterface.addColumn('form16_sap_responses', 'doc_date', { type: DataTypes.STRING(20), allowNull: true }).catch(() => {});
    await queryInterface.addColumn('form16_sap_responses', 'tds_amt', { type: DataTypes.STRING(50), allowNull: true }).catch(() => {});
    await queryInterface.addColumn('form16_sap_responses', 'raw_row', { type: DataTypes.JSONB, allowNull: true }).catch(() => {});
    await queryInterface.addColumn('form16_sap_responses', 'storage_url', { type: DataTypes.STRING(500), allowNull: true }).catch(() => {});

    await queryInterface.removeColumn('form16_sap_responses', 'doc_no').catch(() => {});
    await queryInterface.dropTable('from16_sap_read_file').catch(() => {});
  },
};
