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

export interface Form16DebitNoteAttributes {
  id: number;
  creditNoteId: number;
  debitNoteNumber: string;
  trnsUniqNo?: string;
  sapDocumentNumber?: string;
  amount: number;
  issueDate: Date;
  status: string;
  reason?: string;
  createdBy?: string;
  createdAt: Date;
  updatedAt: Date;
}

interface Form16DebitNoteCreationAttributes
  extends Optional<
    Form16DebitNoteAttributes,
    'id' | 'sapDocumentNumber' | 'reason' | 'createdBy' | 'createdAt' | 'updatedAt'
  > {}

class Form16DebitNote
  extends Model<Form16DebitNoteAttributes, Form16DebitNoteCreationAttributes>
  implements Form16DebitNoteAttributes
{
  public id!: number;
  public creditNoteId!: number;
  public debitNoteNumber!: string;
  public trnsUniqNo?: string;
  public sapDocumentNumber?: string;
  public amount!: number;
  public issueDate!: Date;
  public status!: string;
  public reason?: string;
  public createdBy?: string;
  public createdAt!: Date;
  public updatedAt!: Date;

  public creditNote?: Form16CreditNote;
  public createdByUser?: User;
}

Form16DebitNote.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    creditNoteId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      field: 'credit_note_id',
      references: { model: 'form_16_credit_notes', key: 'id' },
      onDelete: 'CASCADE',
      unique: true,
    },
    debitNoteNumber: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
      field: 'debit_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',
    },
    status: {
      type: DataTypes.STRING(20),
      allowNull: false,
      defaultValue: 'pending',
    },
    reason: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    createdBy: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'created_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_debit_notes',
    timestamps: true,
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
  }
);

Form16DebitNote.belongsTo(Form16CreditNote, {
  as: 'creditNote',
  foreignKey: 'creditNoteId',
  targetKey: 'id',
});

Form16DebitNote.belongsTo(User, {
  as: 'createdByUser',
  foreignKey: 'createdBy',
  targetKey: 'userId',
});

export { Form16DebitNote };
