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

export interface Form16aSubmissionAttributes {
  id: number;
  requestId: string;
  dealerCode: string;
  form16aNumber: string;
  financialYear: string;
  quarter: string;
  version: number;
  tdsAmount: number;
  totalAmount: number;
  tanNumber: string;
  deductorName: string;
  documentUrl?: string;
  ocrExtractedData?: Record<string, unknown> | null;
  status: string;
  validationStatus?: string | null;
  validationNotes?: string | null;
  submittedDate?: Date;
  processedDate?: Date;
  processedBy?: string;
  createdAt: Date;
  updatedAt: Date;
}

interface Form16aSubmissionCreationAttributes
  extends Optional<
    Form16aSubmissionAttributes,
    | 'id'
    | 'documentUrl'
    | 'ocrExtractedData'
    | 'validationStatus'
    | 'validationNotes'
    | 'submittedDate'
    | 'processedDate'
    | 'processedBy'
    | 'createdAt'
    | 'updatedAt'
  > {}

class Form16aSubmission
  extends Model<Form16aSubmissionAttributes, Form16aSubmissionCreationAttributes>
  implements Form16aSubmissionAttributes
{
  public id!: number;
  public requestId!: string;
  public dealerCode!: string;
  public form16aNumber!: string;
  public financialYear!: string;
  public quarter!: string;
  public version!: number;
  public tdsAmount!: number;
  public totalAmount!: number;
  public tanNumber!: string;
  public deductorName!: string;
  public documentUrl?: string;
  public ocrExtractedData?: Record<string, unknown> | null;
  public status!: string;
  public validationStatus?: string;
  public validationNotes?: string;
  public submittedDate?: Date;
  public processedDate?: Date;
  public processedBy?: string;
  public createdAt!: Date;
  public updatedAt!: Date;

  public workflowRequest?: WorkflowRequest;
  public processedByUser?: User;
}

Form16aSubmission.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    requestId: {
      type: DataTypes.UUID,
      allowNull: false,
      unique: true,
      field: 'request_id',
      references: { model: 'workflow_requests', key: 'request_id' },
    },
    dealerCode: {
      type: DataTypes.STRING(50),
      allowNull: false,
      field: 'dealer_code',
    },
    form16aNumber: {
      type: DataTypes.STRING(50),
      allowNull: false,
      field: 'form16a_number',
    },
    financialYear: {
      type: DataTypes.STRING(20),
      allowNull: false,
      field: 'financial_year',
    },
    quarter: {
      type: DataTypes.STRING(10),
      allowNull: false,
    },
    version: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1,
    },
    tdsAmount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
      field: 'tds_amount',
    },
    totalAmount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: false,
      field: 'total_amount',
    },
    tanNumber: {
      type: DataTypes.STRING(20),
      allowNull: false,
      field: 'tan_number',
    },
    deductorName: {
      type: DataTypes.STRING(255),
      allowNull: false,
      field: 'deductor_name',
    },
    documentUrl: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'document_url',
    },
    ocrExtractedData: {
      type: DataTypes.JSONB,
      allowNull: true,
      field: 'ocr_extracted_data',
    },
    status: {
      type: DataTypes.STRING(20),
      allowNull: false,
      defaultValue: 'pending',
    },
    validationStatus: {
      type: DataTypes.STRING(20),
      allowNull: true,
      field: 'validation_status',
    },
    validationNotes: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'validation_notes',
    },
    submittedDate: {
      type: DataTypes.DATE,
      allowNull: true,
      field: 'submitted_date',
    },
    processedDate: {
      type: DataTypes.DATE,
      allowNull: true,
      field: 'processed_date',
    },
    processedBy: {
      type: DataTypes.UUID,
      allowNull: true,
      field: 'processed_by',
      references: { model: 'users', key: 'user_id' },
    },
    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: 'form16a_submissions',
    timestamps: true,
    underscored: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
  }
);

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

Form16aSubmission.belongsTo(User, {
  as: 'processedByUser',
  foreignKey: 'processedBy',
  targetKey: 'userId',
});

export { Form16aSubmission };
