import { MigrationInterface, QueryRunner } from 'typeorm';

/**
 * Contract validity window. When the backoffice accepts a price-confirmed
 * booking, staff define how many days the contract stays valid. The window runs
 * from the accept moment (valid_from) through valid_from + N days (valid_until).
 * Outside that window the contract is considered expired.
 */
export class AddContractValidityWindow1820000000005
  implements MigrationInterface
{
  name = 'AddContractValidityWindow1820000000005';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE freight.bookings ADD COLUMN IF NOT EXISTS contract_validity_days integer;`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.bookings ADD COLUMN IF NOT EXISTS contract_valid_from timestamptz;`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.bookings ADD COLUMN IF NOT EXISTS contract_valid_until timestamptz;`,
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE freight.bookings DROP COLUMN IF EXISTS contract_valid_until;`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.bookings DROP COLUMN IF EXISTS contract_valid_from;`,
    );
    await queryRunner.query(
      `ALTER TABLE freight.bookings DROP COLUMN IF EXISTS contract_validity_days;`,
    );
  }
}
