/**
 * Migration: Add Carrier Columns to CSV Bookings
 *
 * Links bookings to carrier profiles and tracks carrier interactions
 * Including: viewed at, accepted/rejected timestamps, notes, and rejection reason
 */

import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddCarrierToCsvBookings1733187000000 implements MigrationInterface {
  name = 'AddCarrierToCsvBookings1733187000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Add carrier-related columns to csv_bookings
    await queryRunner.query(`
      ALTER TABLE "csv_bookings"
      ADD COLUMN "carrier_id" UUID NULL,
      ADD COLUMN "carrier_viewed_at" TIMESTAMP NULL,
      ADD COLUMN "carrier_accepted_at" TIMESTAMP NULL,
      ADD COLUMN "carrier_rejected_at" TIMESTAMP NULL,
      ADD COLUMN "carrier_rejection_reason" TEXT NULL,
      ADD COLUMN "carrier_notes" TEXT NULL
    `);

    // Add foreign key constraint to carrier_profiles
    await queryRunner.query(`
      ALTER TABLE "csv_bookings"
      ADD CONSTRAINT "fk_csv_bookings_carrier"
      FOREIGN KEY ("carrier_id")
      REFERENCES "carrier_profiles"("id")
      ON DELETE SET NULL
    `);

    // Create index for carrier_id
    await queryRunner.query(`
      CREATE INDEX "idx_csv_bookings_carrier_id" ON "csv_bookings" ("carrier_id")
    `);

    // Create index for carrier interaction timestamps
    await queryRunner.query(`
      CREATE INDEX "idx_csv_bookings_carrier_viewed_at"
      ON "csv_bookings" ("carrier_viewed_at")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_csv_bookings_carrier_accepted_at"
      ON "csv_bookings" ("carrier_accepted_at")
    `);

    // Composite index for carrier bookings queries
    await queryRunner.query(`
      CREATE INDEX "idx_csv_bookings_carrier_status"
      ON "csv_bookings" ("carrier_id", "status")
    `);

    // Add comments
    await queryRunner.query(`
      COMMENT ON COLUMN "csv_bookings"."carrier_id" IS 'Linked carrier profile (transporteur)'
    `);
    await queryRunner.query(`
      COMMENT ON COLUMN "csv_bookings"."carrier_viewed_at" IS 'First time carrier viewed this booking'
    `);
    await queryRunner.query(`
      COMMENT ON COLUMN "csv_bookings"."carrier_accepted_at" IS 'Timestamp when carrier accepted'
    `);
    await queryRunner.query(`
      COMMENT ON COLUMN "csv_bookings"."carrier_rejected_at" IS 'Timestamp when carrier rejected'
    `);
    await queryRunner.query(`
      COMMENT ON COLUMN "csv_bookings"."carrier_rejection_reason" IS 'Reason for rejection (optional)'
    `);
    await queryRunner.query(`
      COMMENT ON COLUMN "csv_bookings"."carrier_notes" IS 'Private notes from carrier'
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // Remove indexes first
    await queryRunner.query(`DROP INDEX IF EXISTS "idx_csv_bookings_carrier_id"`);
    await queryRunner.query(`DROP INDEX IF EXISTS "idx_csv_bookings_carrier_viewed_at"`);
    await queryRunner.query(`DROP INDEX IF EXISTS "idx_csv_bookings_carrier_accepted_at"`);
    await queryRunner.query(`DROP INDEX IF EXISTS "idx_csv_bookings_carrier_status"`);

    // Remove foreign key constraint
    await queryRunner.query(`
      ALTER TABLE "csv_bookings"
      DROP CONSTRAINT IF EXISTS "fk_csv_bookings_carrier"
    `);

    // Remove columns
    await queryRunner.query(`
      ALTER TABLE "csv_bookings"
      DROP COLUMN IF EXISTS "carrier_id",
      DROP COLUMN IF EXISTS "carrier_viewed_at",
      DROP COLUMN IF EXISTS "carrier_accepted_at",
      DROP COLUMN IF EXISTS "carrier_rejected_at",
      DROP COLUMN IF EXISTS "carrier_rejection_reason",
      DROP COLUMN IF EXISTS "carrier_notes"
    `);
  }
}
