import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Booking now captures:
 *   - customs clearing as an explicit flag + the customs clearing agent name
 *     (shown when the service includes customs), and
 *   - first/last-mile pickup & delivery coordinates (lat/lng) alongside the
 *     existing address text, so the map picker can store and restore the pin.
 *
 * Shipping line is no longer collected from the booking form; the column stays
 * for historical data and the (now dormant) shipping-line pricing trigger.
 */
export class AddCustomsAgentAndMileCoordinates1820000000006
  implements MigrationInterface
{
  name = 'AddCustomsAgentAndMileCoordinates1820000000006';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.bookings
        ADD COLUMN IF NOT EXISTS first_mile_pickup_lat numeric(10,7) NULL,
        ADD COLUMN IF NOT EXISTS first_mile_pickup_lng numeric(10,7) NULL,
        ADD COLUMN IF NOT EXISTS last_mile_delivery_lat numeric(10,7) NULL,
        ADD COLUMN IF NOT EXISTS last_mile_delivery_lng numeric(10,7) NULL,
        ADD COLUMN IF NOT EXISTS customs_clearing_enabled boolean NOT NULL DEFAULT false,
        ADD COLUMN IF NOT EXISTS customs_clearing_agent varchar(200) NULL;
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
      ALTER TABLE freight.bookings
        DROP COLUMN IF EXISTS customs_clearing_agent,
        DROP COLUMN IF EXISTS customs_clearing_enabled,
        DROP COLUMN IF EXISTS last_mile_delivery_lng,
        DROP COLUMN IF EXISTS last_mile_delivery_lat,
        DROP COLUMN IF EXISTS first_mile_pickup_lng,
        DROP COLUMN IF EXISTS first_mile_pickup_lat;
    `);
  }
}
