/**
 * Form 16 critical changes:
 * 1. Add upload_log_id to tds_26as_entries to link each record to an upload.
 * 2. form_16_26as_quarter_snapshots: stores aggregated 26AS total per (tan, fy, quarter) per upload version.
 * 3. form_16_quarter_status: current status per (tan, fy, quarter) - SETTLED | DEBIT_ISSUED_PENDING_FORM16.
 * 4. form_16_ledger_entries: full audit trail of every credit/debit (no deletion).
 */

import { QueryInterface, DataTypes } from 'sequelize';

export async function up(queryInterface: QueryInterface): Promise<void> {
  // 1. Add upload_log_id to tds_26as_entries (nullable for existing rows)
  await queryInterface.addColumn('tds_26as_entries', 'upload_log_id', {
    type: DataTypes.INTEGER,
    allowNull: true,
    references: { model: 'form_16_26as_upload_log', key: 'id' },
    onDelete: 'SET NULL',
  });
  await queryInterface.addIndex('tds_26as_entries', ['upload_log_id'], { name: 'idx_tds_26as_upload_log_id' });

  // 2. form_16_26as_quarter_snapshots: one row per "version" of 26AS aggregated total for (tan, fy, quarter)
  await queryInterface.createTable('form_16_26as_quarter_snapshots', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    tan_number: {
      type: DataTypes.STRING(20),
      allowNull: false,
    },
    financial_year: {
      type: DataTypes.STRING(20),
      allowNull: false,
    },
    quarter: {
      type: DataTypes.STRING(10),
      allowNull: false,
    },
    aggregated_amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
      comment: 'Sum of tax_deducted for this tan+fy+quarter (Section 194Q, Booking F/O only)',
    },
    upload_log_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'form_16_26as_upload_log', key: 'id' },
      onDelete: 'SET NULL',
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
  });
  await queryInterface.addIndex('form_16_26as_quarter_snapshots', ['tan_number', 'financial_year', 'quarter'], {
    name: 'idx_form16_26as_snap_tan_fy_qtr',
  });
  await queryInterface.addIndex('form_16_26as_quarter_snapshots', ['upload_log_id'], {
    name: 'idx_form16_26as_snap_upload_log_id',
  });

  // 3. form_16_quarter_status: current status per (tan, fy, quarter) for reverse-first logic
  await queryInterface.createTable('form_16_quarter_status', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    tan_number: {
      type: DataTypes.STRING(20),
      allowNull: false,
    },
    financial_year: {
      type: DataTypes.STRING(20),
      allowNull: false,
    },
    quarter: {
      type: DataTypes.STRING(10),
      allowNull: false,
    },
    status: {
      type: DataTypes.STRING(50),
      allowNull: false,
      comment: 'SETTLED | DEBIT_ISSUED_PENDING_FORM16',
    },
    last_credit_note_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'form_16_credit_notes', key: 'id' },
      onDelete: 'SET NULL',
    },
    last_debit_note_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'form_16_debit_notes', key: 'id' },
      onDelete: 'SET NULL',
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
  });
  await queryInterface.addIndex('form_16_quarter_status', ['tan_number', 'financial_year', 'quarter'], {
    name: 'idx_form16_quarter_status_tan_fy_qtr',
    unique: true,
  });

  // 4. form_16_ledger_entries: full history of every credit and debit (no deletion)
  await queryInterface.createTable('form_16_ledger_entries', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    tan_number: {
      type: DataTypes.STRING(20),
      allowNull: false,
    },
    financial_year: {
      type: DataTypes.STRING(20),
      allowNull: false,
    },
    quarter: {
      type: DataTypes.STRING(10),
      allowNull: false,
    },
    entry_type: {
      type: DataTypes.STRING(10),
      allowNull: false,
      comment: 'CREDIT | DEBIT',
    },
    amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
    },
    credit_note_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'form_16_credit_notes', key: 'id' },
      onDelete: 'SET NULL',
    },
    debit_note_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'form_16_debit_notes', key: 'id' },
      onDelete: 'SET NULL',
    },
    form16_submission_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'form16a_submissions', key: 'id' },
      onDelete: 'SET NULL',
    },
    snapshot_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: { model: 'form_16_26as_quarter_snapshots', key: 'id' },
      onDelete: 'SET NULL',
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
  });
  await queryInterface.addIndex('form_16_ledger_entries', ['tan_number', 'financial_year', 'quarter'], {
    name: 'idx_form16_ledger_tan_fy_qtr',
  });
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.dropTable('form_16_ledger_entries');
  await queryInterface.dropTable('form_16_quarter_status');
  await queryInterface.dropTable('form_16_26as_quarter_snapshots');
  await queryInterface.removeIndex('tds_26as_entries', 'idx_tds_26as_upload_log_id');
  await queryInterface.removeColumn('tds_26as_entries', 'upload_log_id');
}
