import { MigrationInterface, QueryRunner } from 'typeorm';

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

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Add commission columns to csv_bookings (bookings table may not exist yet)
    await queryRunner.query(`
      ALTER TABLE "csv_bookings"
      ADD COLUMN IF NOT EXISTS "commission_rate" DECIMAL(5,2),
      ADD COLUMN IF NOT EXISTS "commission_amount_eur" DECIMAL(12,2)
    `);

    // Only alter bookings table if it exists
    await queryRunner.query(`
      DO $$ BEGIN
        IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'bookings') THEN
          ALTER TABLE "bookings"
          ADD COLUMN "commission_rate" DECIMAL(5,2),
          ADD COLUMN "commission_amount_eur" DECIMAL(12,2);
        END IF;
      END $$;
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE "csv_bookings"
      DROP COLUMN IF EXISTS "commission_amount_eur",
      DROP COLUMN IF EXISTS "commission_rate"
    `);

    await queryRunner.query(`
      DO $$ BEGIN
        IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'bookings') THEN
          ALTER TABLE "bookings"
          DROP COLUMN "commission_amount_eur",
          DROP COLUMN "commission_rate";
        END IF;
      END $$;
    `);
  }
}
