import { MigrationInterface, QueryRunner } from "typeorm";

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

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`
            CREATE TABLE freight.booking_batch_offers (
                id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
                booking_id uuid NOT NULL REFERENCES freight.bookings(id) ON DELETE CASCADE,
                train_schedule_id uuid NOT NULL REFERENCES freight.train_schedules(id) ON DELETE CASCADE,
                offered_wagons integer NOT NULL,
                total_wagons integer NOT NULL,
                offered_lines jsonb NULL,
                offered_weight_tons numeric(12, 3) NOT NULL,
                offered_amount numeric(14, 2) NOT NULL,
                offered_pricing_breakdown jsonb NULL,
                invoice_id uuid NULL,
                payment_deadline timestamptz NOT NULL,
                status varchar(10) NOT NULL DEFAULT 'OFFERED',
                created_at timestamptz NOT NULL DEFAULT now(),
                updated_at timestamptz NOT NULL DEFAULT now(),
                deleted_at timestamptz NULL
            );
        `);
        await queryRunner.query(
            `CREATE INDEX idx_booking_batch_offers_booking ON freight.booking_batch_offers (booking_id);`,
        );
        await queryRunner.query(
            `CREATE INDEX idx_booking_batch_offers_schedule ON freight.booking_batch_offers (train_schedule_id);`,
        );
        await queryRunner.query(
            `CREATE INDEX idx_booking_batch_offers_status ON freight.booking_batch_offers (status);`,
        );
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`DROP TABLE IF EXISTS freight.booking_batch_offers;`);
    }
}
