import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Truck detention support.
 *  - last_mile.arrived_at / delivered_at: the detention window for an EDR
 *    last-mile vehicle. The clock runs from arrival at destination; the customer
 *    has a grace period (default 3h) to clear/return, after which detention
 *    accrues per truck per day until delivered_at (or now, if still out).
 *  - warehouse_fee_rules.free_hours: configurable grace window (hours) for a
 *    TRUCK_DETENTION_FEE rule; null/0 falls back to the 3-hour default.
 */
export class AddTruckDetentionTiming2000000000000 implements MigrationInterface {
  name = 'AddTruckDetentionTiming2000000000000';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE freight.last_mile ADD COLUMN IF NOT EXISTS arrived_at timestamptz`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.last_mile ADD COLUMN IF NOT EXISTS delivered_at timestamptz`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.warehouse_fee_rules ADD COLUMN IF NOT EXISTS free_hours int`,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`ALTER TABLE freight.warehouse_fee_rules DROP COLUMN IF EXISTS free_hours`);
    await queryRunner.query(`ALTER TABLE freight.last_mile DROP COLUMN IF EXISTS delivered_at`);
    await queryRunner.query(`ALTER TABLE freight.last_mile DROP COLUMN IF EXISTS arrived_at`);
  }
}
