import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Moves service-level priority off the service_types table and onto the
 * admin-managed priority_configs table as a new CUSTOMS rule type.
 *
 *  - Drops service_types.priority_bonus_points (replaced by CUSTOMS configs).
 *  - Widens priority_configs.type CHECK to allow 'CUSTOMS' (currency must be
 *    null, same as WAGON).
 *  - Seeds the two customs wagon-count tiers: 1–10 → 7 pts, 11–53 → 15 pts.
 *    CUSTOMS rules apply only when the booking's service type includesCustoms.
 */
export class AddCustomsPriorityConfig2000000000000 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.service_types DROP COLUMN IF EXISTS priority_bonus_points;
    `);

    await queryRunner.query(`
      ALTER TABLE freight.priority_configs
        DROP CONSTRAINT IF EXISTS priority_configs_type_check;
    `);
    await queryRunner.query(`
      ALTER TABLE freight.priority_configs
        ADD CONSTRAINT priority_configs_type_check
        CHECK (type IN ('WAGON', 'CURRENCY', 'CUSTOMS'));
    `);

    await queryRunner.query(`
      ALTER TABLE freight.priority_configs
        DROP CONSTRAINT IF EXISTS chk_currency_for_type;
    `);
    await queryRunner.query(`
      ALTER TABLE freight.priority_configs
        ADD CONSTRAINT chk_currency_for_type CHECK (
          (type = 'WAGON' AND currency IS NULL) OR
          (type = 'CURRENCY' AND currency IS NOT NULL) OR
          (type = 'CUSTOMS' AND currency IS NULL)
        );
    `);

    await queryRunner.query(`
      INSERT INTO freight.priority_configs
        (type, label, currency, min_wagon_count, max_wagon_count, score_points, is_active, display_order)
      VALUES
        ('CUSTOMS', 'With customs 1–10 wagons', NULL, 1, 10, 7, true, 1),
        ('CUSTOMS', 'With customs 11–53 wagons', NULL, 11, 53, 15, true, 2);
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      DELETE FROM freight.priority_configs WHERE type = 'CUSTOMS';
    `);

    await queryRunner.query(`
      ALTER TABLE freight.priority_configs
        DROP CONSTRAINT IF EXISTS chk_currency_for_type;
    `);
    await queryRunner.query(`
      ALTER TABLE freight.priority_configs
        ADD CONSTRAINT chk_currency_for_type CHECK (
          (type = 'WAGON' AND currency IS NULL) OR
          (type = 'CURRENCY' AND currency IS NOT NULL)
        );
    `);

    await queryRunner.query(`
      ALTER TABLE freight.priority_configs
        DROP CONSTRAINT IF EXISTS priority_configs_type_check;
    `);
    await queryRunner.query(`
      ALTER TABLE freight.priority_configs
        ADD CONSTRAINT priority_configs_type_check
        CHECK (type IN ('WAGON', 'CURRENCY'));
    `);

    await queryRunner.query(`
      ALTER TABLE freight.service_types
        ADD COLUMN IF NOT EXISTS priority_bonus_points INT NOT NULL DEFAULT 0;
    `);
  }
}
