/**
 * Form 16 integration: form16a_submissions (linked to workflow_requests) and form_16_credit_notes.
 * Single workflow DB; Form16 submissions appear in Open/Closed requests via workflow_requests row.
 */

import { QueryInterface, DataTypes } from 'sequelize';

export async function up(queryInterface: QueryInterface): Promise<void> {
  // 1. form16a_submissions: one row per Form 16A submission; request_id links to workflow_requests
  await queryInterface.createTable('form16a_submissions', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    request_id: {
      type: DataTypes.UUID,
      allowNull: false,
      references: { model: 'workflow_requests', key: 'request_id' },
      onDelete: 'CASCADE',
      unique: true,
    },
    dealer_code: {
      type: DataTypes.STRING(50),
      allowNull: false,
    },
    form16a_number: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
    },
    financial_year: {
      type: DataTypes.STRING(20),
      allowNull: false,
    },
    quarter: {
      type: DataTypes.STRING(10),
      allowNull: false,
    },
    version: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1,
    },
    tds_amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
    },
    total_amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
    },
    tan_number: {
      type: DataTypes.STRING(20),
      allowNull: false,
    },
    deductor_name: {
      type: DataTypes.STRING(255),
      allowNull: false,
    },
    document_url: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    status: {
      type: DataTypes.STRING(20),
      allowNull: false,
      defaultValue: 'pending',
    },
    validation_status: {
      type: DataTypes.STRING(20),
      allowNull: true,
    },
    validation_notes: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    submitted_date: {
      type: DataTypes.DATE,
      allowNull: true,
    },
    processed_date: {
      type: DataTypes.DATE,
      allowNull: true,
    },
    processed_by: {
      type: DataTypes.UUID,
      allowNull: true,
      references: { model: 'users', key: 'user_id' },
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
  });

  await queryInterface.addIndex('form16a_submissions', ['request_id'], { name: 'idx_form16a_submissions_request_id' });
  await queryInterface.addIndex('form16a_submissions', ['dealer_code'], { name: 'idx_form16a_submissions_dealer_code' });
  await queryInterface.addIndex('form16a_submissions', ['status'], { name: 'idx_form16a_submissions_status' });
  await queryInterface.addIndex('form16a_submissions', ['financial_year', 'quarter'], { name: 'idx_form16a_submissions_fy_quarter' });

  // 2. form_16_credit_notes: credit notes generated (e.g. via SAP); linked to form16a_submissions
  await queryInterface.createTable('form_16_credit_notes', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    submission_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'form16a_submissions', key: 'id' },
      onDelete: 'CASCADE',
    },
    credit_note_number: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
    },
    sap_document_number: {
      type: DataTypes.STRING(50),
      allowNull: true,
    },
    amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
    },
    issue_date: {
      type: DataTypes.DATEONLY,
      allowNull: false,
    },
    financial_year: {
      type: DataTypes.STRING(20),
      allowNull: false,
    },
    quarter: {
      type: DataTypes.STRING(10),
      allowNull: false,
    },
    status: {
      type: DataTypes.STRING(20),
      allowNull: false,
      defaultValue: 'pending',
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    issued_by: {
      type: DataTypes.UUID,
      allowNull: true,
      references: { model: 'users', key: 'user_id' },
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
  });

  await queryInterface.addIndex('form_16_credit_notes', ['submission_id'], { name: 'idx_form_16_credit_notes_submission_id' });
  await queryInterface.addIndex('form_16_credit_notes', ['status'], { name: 'idx_form_16_credit_notes_status' });
}

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