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

export interface Form16CreditNoteAttributes {
  id: number;
  submissionId: number;
  creditNoteNumber: string;
  trnsUniqNo?: string;
  sapDocumentNumber?: string;
  amount: number;
  issueDate: Date;
  financialYear: string;
  quarter: string;
  status: string;
  remarks?: string;
  issuedBy?: string;
  createdAt: Date;
  updatedAt: Date;
}

interface Form16CreditNoteCreationAttributes
  extends Optional<
    Form16CreditNoteAttributes,
    'id' | 'sapDocumentNumber' | 'remarks' | 'issuedBy' | 'createdAt' | 'updatedAt'
  > {}

class Form16CreditNote
  extends Model<Form16CreditNoteAttributes, Form16CreditNoteCreationAttributes>
  implements Form16CreditNoteAttributes
{
  public id!: number;
  public submissionId!: number;
  public creditNoteNumber!: string;
  public trnsUniqNo?: string;
  public sapDocumentNumber?: string;
  public amount!: number;
  public issueDate!: Date;
  public financialYear!: string;
  public quarter!: string;
  public status!: string;
  public remarks?: string;
  public issuedBy?: string;
  public createdAt!: Date;
  public updatedAt!: Date;

  public submission?: Form16aSubmission;
  public issuedByUser?: User;
}

Form16CreditNote.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    submissionId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      field: 'submission_id',
      references: { model: 'form16a_submissions', key: 'id' },
    },
    creditNoteNumber: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
      field: 'credit_note_number',
    },
    trnsUniqNo: {
      type: DataTypes.STRING(120),
      allowNull: true,
      field: 'trns_uniq_no',
    },
    sapDocumentNumber: {
      type: DataTypes.STRING(50),
      allowNull: true,
      field: 'sap_document_number',
    },
    amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
    },
    issueDate: {
      type: DataTypes.DATEONLY,
      allowNull: false,
      field: 'issue_date',
    },
    financialYear: {
      type: DataTypes.STRING(20),
      allowNull: false,
      field: 'financial_year',
    },
    quarter: {
      type: DataTypes.STRING(10),
      allowNull: false,
    },
    status: {
      type: DataTypes.STRING(20),
      allowNull: false,
      defaultValue: 'pending',
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    issuedBy: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'issued_by',
      references: { model: 'users', key: 'user_id' },
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      field: 'created_at',
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      field: 'updated_at',
    },
  },
  {
    sequelize,
    tableName: 'form_16_credit_notes',
    timestamps: true,
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
  }
);

Form16CreditNote.belongsTo(Form16aSubmission, {
  as: 'submission',
  foreignKey: 'submissionId',
  targetKey: 'id',
});

Form16CreditNote.belongsTo(User, {
  as: 'issuedByUser',
  foreignKey: 'issuedBy',
  targetKey: 'userId',
});

export { Form16CreditNote };
