import { DataTypes, Model, Optional } from 'sequelize';
import { sequelize } from '@config/database';
import { Form16CreditNote } from './Form16CreditNote';
import { Form16DebitNote } from './Form16DebitNote';
import { Form16aSubmission } from './Form16aSubmission';
import { Form1626asQuarterSnapshot } from './Form1626asQuarterSnapshot';

export type Form16LedgerEntryType = 'CREDIT' | 'DEBIT';

export interface Form16LedgerEntryAttributes {
  id: number;
  tanNumber: string;
  financialYear: string;
  quarter: string;
  entryType: Form16LedgerEntryType;
  amount: number;
  creditNoteId?: number | null;
  debitNoteId?: number | null;
  form16SubmissionId?: number | null;
  snapshotId?: number | null;
  createdAt: Date;
}

interface Form16LedgerEntryCreationAttributes
  extends Optional<
    Form16LedgerEntryAttributes,
    'id' | 'creditNoteId' | 'debitNoteId' | 'form16SubmissionId' | 'snapshotId' | 'createdAt'
  > {}

class Form16LedgerEntry
  extends Model<Form16LedgerEntryAttributes, Form16LedgerEntryCreationAttributes>
  implements Form16LedgerEntryAttributes
{
  public id!: number;
  public tanNumber!: string;
  public financialYear!: string;
  public quarter!: string;
  public entryType!: Form16LedgerEntryType;
  public amount!: number;
  public creditNoteId?: number | null;
  public debitNoteId?: number | null;
  public form16SubmissionId?: number | null;
  public snapshotId?: number | null;
  public createdAt!: Date;

  public creditNote?: Form16CreditNote;
  public debitNote?: Form16DebitNote;
  public form16Submission?: Form16aSubmission;
  public snapshot?: Form1626asQuarterSnapshot;
}

Form16LedgerEntry.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    tanNumber: {
      type: DataTypes.STRING(20),
      allowNull: false,
      field: 'tan_number',
    },
    financialYear: {
      type: DataTypes.STRING(20),
      allowNull: false,
      field: 'financial_year',
    },
    quarter: {
      type: DataTypes.STRING(10),
      allowNull: false,
    },
    entryType: {
      type: DataTypes.STRING(10),
      allowNull: false,
      field: 'entry_type',
    },
    amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
    },
    creditNoteId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'credit_note_id',
      references: { model: 'form_16_credit_notes', key: 'id' },
      onDelete: 'SET NULL',
    },
    debitNoteId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'debit_note_id',
      references: { model: 'form_16_debit_notes', key: 'id' },
      onDelete: 'SET NULL',
    },
    form16SubmissionId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'form16_submission_id',
      references: { model: 'form16a_submissions', key: 'id' },
      onDelete: 'SET NULL',
    },
    snapshotId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'snapshot_id',
      references: { model: 'form_16_26as_quarter_snapshots', key: 'id' },
      onDelete: 'SET NULL',
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      field: 'created_at',
    },
  },
  {
    sequelize,
    tableName: 'form_16_ledger_entries',
    timestamps: false,
    underscored: true,
  }
);

Form16LedgerEntry.belongsTo(Form16CreditNote, { as: 'creditNote', foreignKey: 'creditNoteId', targetKey: 'id' });
Form16LedgerEntry.belongsTo(Form16DebitNote, { as: 'debitNote', foreignKey: 'debitNoteId', targetKey: 'id' });
Form16LedgerEntry.belongsTo(Form16aSubmission, {
  as: 'form16Submission',
  foreignKey: 'form16SubmissionId',
  targetKey: 'id',
});
Form16LedgerEntry.belongsTo(Form1626asQuarterSnapshot, {
  as: 'snapshot',
  foreignKey: 'snapshotId',
  targetKey: 'id',
});

export { Form16LedgerEntry };
