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

interface ClaimInvoiceAttributes {
  invoiceId: string;
  requestId: string;
  invoiceNumber?: string;
  invoiceDate?: Date;
  amount?: number;
  dmsNumber?: string;
  invoiceFilePath?: string;
  status?: string;
  errorMessage?: string;
  generatedAt?: Date;
  description?: string;
  createdAt: Date;
  updatedAt: Date;
}

interface ClaimInvoiceCreationAttributes extends Optional<ClaimInvoiceAttributes, 'invoiceId' | 'invoiceNumber' | 'invoiceDate' | 'amount' | 'dmsNumber' | 'invoiceFilePath' | 'status' | 'errorMessage' | 'generatedAt' | 'description' | 'createdAt' | 'updatedAt'> {}

class ClaimInvoice extends Model<ClaimInvoiceAttributes, ClaimInvoiceCreationAttributes> implements ClaimInvoiceAttributes {
  public invoiceId!: string;
  public requestId!: string;
  public invoiceNumber?: string;
  public invoiceDate?: Date;
  public amount?: number;
  public dmsNumber?: string;
  public invoiceFilePath?: string;
  public status?: string;
  public errorMessage?: string;
  public generatedAt?: Date;
  public description?: string;
  public createdAt!: Date;
  public updatedAt!: Date;
}

ClaimInvoice.init(
  {
    invoiceId: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'invoice_id',
    },
    requestId: {
      type: DataTypes.UUID,
      allowNull: false,
      unique: true,
      field: 'request_id',
      references: {
        model: 'workflow_requests',
        key: 'request_id',
      },
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    },
    invoiceNumber: {
      type: DataTypes.STRING(100),
      allowNull: true,
      field: 'invoice_number',
    },
    invoiceDate: {
      type: DataTypes.DATEONLY,
      allowNull: true,
      field: 'invoice_date',
    },
    amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
      field: 'invoice_amount',
    },
    dmsNumber: {
      type: DataTypes.STRING(100),
      allowNull: true,
      field: 'dms_number',
    },
    invoiceFilePath: {
      type: DataTypes.STRING(500),
      allowNull: true,
      field: 'invoice_file_path',
    },
    status: {
      type: DataTypes.STRING(50),
      allowNull: true,
      field: 'generation_status',
    },
    errorMessage: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'error_message',
    },
    generatedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      field: 'generated_at',
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'description',
    },
    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,
    modelName: 'ClaimInvoice',
    tableName: 'claim_invoices',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    indexes: [
      { unique: true, fields: ['request_id'], name: 'idx_claim_invoices_request_id' },
      { fields: ['invoice_number'], name: 'idx_claim_invoices_invoice_number' },
      { fields: ['dms_number'], name: 'idx_claim_invoices_dms_number' },
      { fields: ['generation_status'], name: 'idx_claim_invoices_status' },
    ],
  }
);

WorkflowRequest.hasOne(ClaimInvoice, {
  as: 'claimInvoice',
  foreignKey: 'requestId',
  sourceKey: 'requestId',
});

ClaimInvoice.belongsTo(WorkflowRequest, {
  as: 'workflowRequest',
  foreignKey: 'requestId',
  targetKey: 'requestId',
});

// Note: hasMany association with ClaimCreditNote is defined in ClaimCreditNote.ts
// to avoid circular dependency issues

export { ClaimInvoice };

