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

interface CpcDocumentAttributes {
  id: string;
  bookingId?: string;
  claimId?: string;
  attemptNo?: number;
  documentType?: string;
  documentGcpUrl?: string;
  provider?: string;
  msdPayload?: any;
  extractedFields?: any;
  fieldConfidence?: any;
  validationStatus?: string;
  matchPercentage?: number;
  mismatchReasons?: any;
  fieldResults?: any;
  ipAddress?: string;
  createdAt?: Date;
}

interface CpcDocumentCreationAttributes extends Optional<CpcDocumentAttributes, 'id' | 'attemptNo' | 'createdAt'> {}

class CpcDocument extends Model<CpcDocumentAttributes, CpcDocumentCreationAttributes> implements CpcDocumentAttributes {
  public id!: string;
  public bookingId?: string;
  public claimId?: string;
  public attemptNo?: number;
  public documentType?: string;
  public documentGcpUrl?: string;
  public provider?: string;
  public msdPayload?: any;
  public extractedFields?: any;
  public fieldConfidence?: any;
  public validationStatus?: string;
  public matchPercentage?: number;
  public mismatchReasons?: any;
  public fieldResults?: any;
  public ipAddress?: string;
  public createdAt!: Date;
}

CpcDocument.init(
  {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      field: 'id'
    },
    bookingId: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'booking_id'
    },
    claimId: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'claim_id'
    },
    attemptNo: {
      type: DataTypes.INTEGER,
      defaultValue: 1,
      field: 'attempt_no'
    },
    documentType: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'document_type'
    },
    documentGcpUrl: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'document_gcp_url'
    },
    provider: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'provider'
    },
    msdPayload: {
      type: DataTypes.JSONB,
      allowNull: true,
      field: 'msd_payload'
    },
    extractedFields: {
      type: DataTypes.JSONB,
      allowNull: true,
      field: 'extracted_fields'
    },
    fieldConfidence: {
      type: DataTypes.JSONB,
      allowNull: true,
      field: 'field_confidence'
    },
    validationStatus: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'validation_status'
    },
    matchPercentage: {
      type: DataTypes.FLOAT,
      allowNull: true,
      field: 'match_percentage'
    },
    mismatchReasons: {
      type: DataTypes.JSONB,
      allowNull: true,
      field: 'mismatch_reasons'
    },
    fieldResults: {
      type: DataTypes.JSONB,
      allowNull: true,
      field: 'field_results'
    },
    ipAddress: {
      type: DataTypes.STRING(255),
      allowNull: true,
      field: 'ip_address'
    },
    createdAt: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      field: 'created_at'
    }
  },
  {
    sequelize,
    modelName: 'CpcDocument',
    tableName: 'cpc_documents',
    timestamps: false,
    indexes: [
      {
        name: 'unique_cpc_document_claim_attempt_booking',
        unique: true,
        fields: ['claimId', 'attemptNo', 'bookingId']
      }
    ]
  }
);

export { CpcDocument };
