import { QueryInterface, DataTypes } from 'sequelize';

export async function up(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.createTable('claim_invoices', {
    invoice_id: {
      type: DataTypes.UUID,
      primaryKey: true,
      defaultValue: DataTypes.UUIDV4,
    },
    request_id: {
      type: DataTypes.UUID,
      allowNull: false,
      unique: true, // one invoice per request (adjust later if multiples needed)
      references: { model: 'workflow_requests', key: 'request_id' },
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    },
    invoice_number: {
      type: DataTypes.STRING(100),
      allowNull: true,
    },
    invoice_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
    },
    invoice_amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
    },
    dms_number: {
      type: DataTypes.STRING(100),
      allowNull: true,
    },
    invoice_file_path: {
      type: DataTypes.STRING(500),
      allowNull: true,
    },
    generation_status: {
      type: DataTypes.STRING(50), // e.g., PENDING, GENERATED, SENT, FAILED, CANCELLED
      allowNull: true,
    },
    error_message: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    generated_at: {
      type: DataTypes.DATE,
      allowNull: true,
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
  });

  await queryInterface.addIndex('claim_invoices', ['request_id'], { name: 'idx_claim_invoices_request_id', unique: true });
  await queryInterface.addIndex('claim_invoices', ['invoice_number'], { name: 'idx_claim_invoices_invoice_number' });
  await queryInterface.addIndex('claim_invoices', ['dms_number'], { name: 'idx_claim_invoices_dms_number' });
  await queryInterface.addIndex('claim_invoices', ['generation_status'], { name: 'idx_claim_invoices_status' });

  await queryInterface.createTable('claim_credit_notes', {
    credit_note_id: {
      type: DataTypes.UUID,
      primaryKey: true,
      defaultValue: DataTypes.UUIDV4,
    },
    request_id: {
      type: DataTypes.UUID,
      allowNull: false,
      unique: true, // one credit note per request (adjust later if multiples needed)
      references: { model: 'workflow_requests', key: 'request_id' },
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    },
    invoice_id: {
      type: DataTypes.UUID,
      allowNull: true,
      references: { model: 'claim_invoices', key: 'invoice_id' },
      onDelete: 'SET NULL',
      onUpdate: 'CASCADE',
    },
    credit_note_number: {
      type: DataTypes.STRING(100),
      allowNull: true,
    },
    credit_note_date: {
      type: DataTypes.DATEONLY,
      allowNull: true,
    },
    credit_amount: {
      type: DataTypes.DECIMAL(15, 2),
      allowNull: true,
    },
    sap_document_number: {
      type: DataTypes.STRING(100),
      allowNull: true,
    },
    credit_note_file_path: {
      type: DataTypes.STRING(500),
      allowNull: true,
    },
    confirmation_status: {
      type: DataTypes.STRING(50), // e.g., PENDING, GENERATED, CONFIRMED, FAILED, CANCELLED
      allowNull: true,
    },
    error_message: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    confirmed_by: {
      type: DataTypes.UUID,
      allowNull: true,
      references: { model: 'users', key: 'user_id' },
      onDelete: 'SET NULL',
      onUpdate: 'CASCADE',
    },
    confirmed_at: {
      type: DataTypes.DATE,
      allowNull: true,
    },
    reason: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    description: {
      type: DataTypes.TEXT,
      allowNull: true,
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
    },
  });

  await queryInterface.addIndex('claim_credit_notes', ['request_id'], { name: 'idx_claim_credit_notes_request_id', unique: true });
  await queryInterface.addIndex('claim_credit_notes', ['invoice_id'], { name: 'idx_claim_credit_notes_invoice_id' });
  await queryInterface.addIndex('claim_credit_notes', ['credit_note_number'], { name: 'idx_claim_credit_notes_number' });
  await queryInterface.addIndex('claim_credit_notes', ['sap_document_number'], { name: 'idx_claim_credit_notes_sap_doc' });
  await queryInterface.addIndex('claim_credit_notes', ['confirmation_status'], { name: 'idx_claim_credit_notes_status' });
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.dropTable('claim_credit_notes');
  await queryInterface.dropTable('claim_invoices');
}

