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

export interface Form16DebitNoteSapResponseAttributes {
  id: number;
  fileName: string;
  debitNoteId?: number | null;
  // Well-known SAP CSV columns stored as individual fields
  trnsUniqNo?: string | null;       // TRNS_UNIQ_NO  – our unique ID echoed back by SAP
  tdsTransId?: string | null;       // TDS_TRNS_ID   – credit note number we sent (primary match key)
  claimNumber?: string | null;      // CLAIM_NUMBER
  sapDocumentNumber?: string | null;// DOC_NO        – SAP-generated document number
  msgTyp?: string | null;           // MSG_TYP
  message?: string | null;          // MESSAGE
  docDate?: string | null;          // DOC_DATE
  tdsAmt?: string | null;           // TDS_AMT
  rawRow?: Record<string, unknown> | null; // any extra / unknown columns from the CSV
  storageUrl?: string | null;
  createdAt: Date;
  updatedAt: Date;
}

interface Form16DebitNoteSapResponseCreationAttributes
  extends Optional<
    Form16DebitNoteSapResponseAttributes,
    | 'id'
    | 'debitNoteId'
    | 'trnsUniqNo'
    | 'tdsTransId'
    | 'claimNumber'
    | 'sapDocumentNumber'
    | 'msgTyp'
    | 'message'
    | 'docDate'
    | 'tdsAmt'
    | 'rawRow'
    | 'storageUrl'
    | 'createdAt'
    | 'updatedAt'
  > {}

class Form16DebitNoteSapResponse
  extends Model<Form16DebitNoteSapResponseAttributes, Form16DebitNoteSapResponseCreationAttributes>
  implements Form16DebitNoteSapResponseAttributes
{
  public id!: number;
  public fileName!: string;
  public debitNoteId?: number | null;
  public trnsUniqNo?: string | null;
  public tdsTransId?: string | null;
  public claimNumber?: string | null;
  public sapDocumentNumber?: string | null;
  public msgTyp?: string | null;
  public message?: string | null;
  public docDate?: string | null;
  public tdsAmt?: string | null;
  public rawRow?: Record<string, unknown> | null;
  public storageUrl?: string | null;
  public createdAt!: Date;
  public updatedAt!: Date;

  public debitNote?: Form16DebitNote;
}

Form16DebitNoteSapResponse.init(
  {
    id:               { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true },
    fileName:         { type: DataTypes.STRING(255), allowNull: false, unique: true, field: 'file_name' },
    debitNoteId:      { type: DataTypes.INTEGER, allowNull: true, field: 'debit_note_id' },
    trnsUniqNo:       { type: DataTypes.STRING(200), allowNull: true, field: 'trns_uniq_no' },
    tdsTransId:       { type: DataTypes.STRING(200), allowNull: true, field: 'tds_trns_id' },
    claimNumber:      { type: DataTypes.STRING(200), allowNull: true, field: 'claim_number' },
    sapDocumentNumber:{ type: DataTypes.STRING(100), allowNull: true, field: 'sap_document_number' },
    msgTyp:           { type: DataTypes.STRING(20),  allowNull: true, field: 'msg_typ' },
    message:          { type: DataTypes.TEXT,        allowNull: true },
    docDate:          { type: DataTypes.STRING(20),  allowNull: true, field: 'doc_date' },
    tdsAmt:           { type: DataTypes.STRING(50),  allowNull: true, field: 'tds_amt' },
    rawRow:           { type: DataTypes.JSONB,       allowNull: true, field: 'raw_row' },
    storageUrl:       { type: DataTypes.STRING(500), allowNull: true, field: 'storage_url' },
    createdAt:        { type: DataTypes.DATE, allowNull: false, field: 'created_at' },
    updatedAt:        { type: DataTypes.DATE, allowNull: false, field: 'updated_at' },
  },
  {
    sequelize,
    tableName: 'form16_debit_note_sap_responses',
    timestamps: true,
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
  }
);

Form16DebitNoteSapResponse.belongsTo(Form16DebitNote, {
  as: 'debitNote',
  foreignKey: 'debitNoteId',
  targetKey: 'id',
});

export { Form16DebitNoteSapResponse };
