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

export interface Tds26asEntryAttributes {
  id: number;
  panNumber?: string;
  tanNumber: string;
  deductorName?: string;
  quarter: string;
  assessmentYear?: string;
  financialYear: string;
  sectionCode?: string;
  amountPaid?: number;
  taxDeducted: number;
  totalTdsDeposited?: number;
  natureOfPayment?: string;
  transactionDate?: string;
  dateOfBooking?: string;
  statusOltas?: string;
  remarks?: string;
  uploadLogId?: number | null;
  createdAt: Date;
  updatedAt: Date;
}

interface Tds26asEntryCreationAttributes
  extends Optional<
    Tds26asEntryAttributes,
    | 'id'
    | 'panNumber'
    | 'deductorName'
    | 'assessmentYear'
    | 'sectionCode'
    | 'amountPaid'
    | 'totalTdsDeposited'
    | 'natureOfPayment'
    | 'transactionDate'
    | 'dateOfBooking'
    | 'statusOltas'
    | 'remarks'
    | 'uploadLogId'
    | 'createdAt'
    | 'updatedAt'
  > {}

class Tds26asEntry
  extends Model<Tds26asEntryAttributes, Tds26asEntryCreationAttributes>
  implements Tds26asEntryAttributes
{
  public id!: number;
  public panNumber?: string;
  public tanNumber!: string;
  public deductorName?: string;
  public quarter!: string;
  public assessmentYear?: string;
  public financialYear!: string;
  public sectionCode?: string;
  public amountPaid?: number;
  public taxDeducted!: number;
  public totalTdsDeposited?: number;
  public natureOfPayment?: string;
  public transactionDate?: string;
  public dateOfBooking?: string;
  public statusOltas?: string;
  public remarks?: string;
  public uploadLogId?: number | null;
  public createdAt!: Date;
  public updatedAt!: Date;
}

Tds26asEntry.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    panNumber: {
      type: DataTypes.STRING(20),
      allowNull: true,
      field: 'pan_number',
    },
    tanNumber: {
      type: DataTypes.STRING(20),
      allowNull: false,
      field: 'tan_number',
    },
    deductorName: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'deductor_name',
    },
    quarter: {
      type: DataTypes.STRING(10),
      allowNull: false,
    },
    assessmentYear: {
      type: DataTypes.STRING(20),
      allowNull: true,
      field: 'assessment_year',
    },
    financialYear: {
      type: DataTypes.STRING(20),
      allowNull: false,
      field: 'financial_year',
    },
    sectionCode: {
      type: DataTypes.STRING(20),
      allowNull: true,
      field: 'section_code',
    },
    amountPaid: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'amount_paid',
    },
    taxDeducted: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
      defaultValue: 0,
      field: 'tax_deducted',
    },
    totalTdsDeposited: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'total_tds_deposited',
    },
    natureOfPayment: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'nature_of_payment',
    },
    transactionDate: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      field: 'transaction_date',
    },
    dateOfBooking: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      field: 'date_of_booking',
    },
    statusOltas: {
      type: DataTypes.STRING(50),
      allowNull: true,
      field: 'status_oltas',
    },
    remarks: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    uploadLogId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      field: 'upload_log_id',
      references: { model: 'form_16_26as_upload_log', key: 'id' },
      onDelete: 'SET NULL',
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'created_at',
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      field: 'updated_at',
    },
  },
  {
    sequelize,
    tableName: 'tds_26as_entries',
    timestamps: true,
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
  }
);

export { Tds26asEntry };
