import { QueryInterface } from 'sequelize';

/**
 * Batch upload can include multiple files of the same document_type in one attempt.
 * Replace unique(claim_id, attempt_no, document_type) with unique(claim_id, attempt_no, booking_id)
 * because booking_id is distinct per file (e.g. CLAIM-1, CLAIM-2, ...).
 */
export async function up(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.sequelize.query(`
    DROP INDEX IF EXISTS unique_cpc_document_attempt;
  `);
  await queryInterface.sequelize.query(`
    CREATE UNIQUE INDEX IF NOT EXISTS unique_cpc_document_claim_attempt_booking
    ON cpc_documents (claim_id, attempt_no, booking_id);
  `);
}

export async function down(queryInterface: QueryInterface): Promise<void> {
  await queryInterface.sequelize.query(`
    DROP INDEX IF EXISTS unique_cpc_document_claim_attempt_booking;
  `);
  await queryInterface.sequelize.query(`
    CREATE UNIQUE INDEX IF NOT EXISTS unique_cpc_document_attempt
    ON cpc_documents (claim_id, attempt_no, document_type);
  `);
}
