import { MigrationInterface, QueryRunner } from "typeorm";

export class AddScheduleWindowPhases1862000000000 implements MigrationInterface {
    name = "AddScheduleWindowPhases1862000000000";

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            ALTER TABLE freight.train_schedules
                ADD COLUMN window_phase varchar(20) NULL,
                ADD COLUMN window_opens_at timestamptz NULL,
                ADD COLUMN window_closes_at timestamptz NULL,
                ADD COLUMN doc_review_ends_at timestamptz NULL,
                ADD COLUMN doc_review_completed_at timestamptz NULL,
                ADD COLUMN payment_phase_ends_at timestamptz NULL,
                ADD COLUMN booking_cycle_no integer NOT NULL DEFAULT 0;
        `);
        await queryRunner.query(`
            CREATE INDEX idx_train_schedules_window_phase
                ON freight.train_schedules (window_phase)
                WHERE window_phase IS NOT NULL;
        `);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`DROP INDEX IF EXISTS freight.idx_train_schedules_window_phase;`);
        await queryRunner.query(`
            ALTER TABLE freight.train_schedules
                DROP COLUMN IF EXISTS window_phase,
                DROP COLUMN IF EXISTS window_opens_at,
                DROP COLUMN IF EXISTS window_closes_at,
                DROP COLUMN IF EXISTS doc_review_ends_at,
                DROP COLUMN IF EXISTS doc_review_completed_at,
                DROP COLUMN IF EXISTS payment_phase_ends_at,
                DROP COLUMN IF EXISTS booking_cycle_no;
        `);
    }
}
