/**
 * Form 16 – 26AS TDS entries table for RE admin.
 * Stores TDS credit data from 26AS (Income Tax portal) for validation against Form 16A submissions.
 */

import { QueryInterface, DataTypes } from 'sequelize';

export async function up(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.createTable('tds_26as_entries', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    tan_number: {
      type: DataTypes.STRING(20),
      allowNull: false,
      comment: 'TAN of deductor',
    },
    deductor_name: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    quarter: {
      type: DataTypes.STRING(10),
      allowNull: false,
      comment: 'Q1, Q2, Q3, Q4',
    },
    assessment_year: {
      type: DataTypes.STRING(20),
      allowNull: true,
      comment: 'e.g. 2024-25',
    },
    financial_year: {
      type: DataTypes.STRING(20),
      allowNull: false,
      comment: 'e.g. 2024-25',
    },
    section_code: {
      type: DataTypes.STRING(20),
      allowNull: true,
      comment: 'e.g. 194C, 194A',
    },
    amount_paid: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
    },
    tax_deducted: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
      defaultValue: 0,
    },
    total_tds_deposited: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
    },
    nature_of_payment: {
      type: DataTypes.STRING(255),
      allowNull: true,
    },
    transaction_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
    },
    date_of_booking: {
      type: DataTypes.DATEONLY,
      allowNull: true,
    },
    status_oltas: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: 'Status of matching with OLTAS',
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
  });

  await queryInterface.addIndex('tds_26as_entries', ['tan_number'], { name: 'idx_tds_26as_tan' });
  await queryInterface.addIndex('tds_26as_entries', ['financial_year', 'quarter'], { name: 'idx_tds_26as_fy_quarter' });
  await queryInterface.addIndex('tds_26as_entries', ['financial_year'], { name: 'idx_tds_26as_fy' });
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.dropTable('tds_26as_entries');
}
