/**
 * Migration: Create RateQuotes Table
 */

import { MigrationInterface, QueryRunner } from 'typeorm';

export class CreateRateQuotes1730000000005 implements MigrationInterface {
  name = 'CreateRateQuotes1730000000005';

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Create rate_quotes table
    await queryRunner.query(`
      CREATE TABLE "rate_quotes" (
        "id" UUID NOT NULL DEFAULT uuid_generate_v4(),
        "carrier_id" UUID NOT NULL,
        "carrier_name" VARCHAR(255) NOT NULL,
        "carrier_code" VARCHAR(50) NOT NULL,
        "origin_code" CHAR(5) NOT NULL,
        "origin_name" VARCHAR(255) NOT NULL,
        "origin_country" VARCHAR(100) NOT NULL,
        "destination_code" CHAR(5) NOT NULL,
        "destination_name" VARCHAR(255) NOT NULL,
        "destination_country" VARCHAR(100) NOT NULL,
        "base_freight" DECIMAL(10,2) NOT NULL,
        "surcharges" JSONB NOT NULL DEFAULT '[]',
        "total_amount" DECIMAL(10,2) NOT NULL,
        "currency" CHAR(3) NOT NULL,
        "container_type" VARCHAR(20) NOT NULL,
        "mode" VARCHAR(10) NOT NULL,
        "etd" TIMESTAMP NOT NULL,
        "eta" TIMESTAMP NOT NULL,
        "transit_days" INTEGER NOT NULL,
        "route" JSONB NOT NULL,
        "availability" INTEGER NOT NULL,
        "frequency" VARCHAR(50) NOT NULL,
        "vessel_type" VARCHAR(100) NULL,
        "co2_emissions_kg" INTEGER NULL,
        "valid_until" TIMESTAMP NOT NULL,
        "created_at" TIMESTAMP NOT NULL DEFAULT NOW(),
        "updated_at" TIMESTAMP NOT NULL DEFAULT NOW(),
        CONSTRAINT "pk_rate_quotes" PRIMARY KEY ("id"),
        CONSTRAINT "fk_rate_quotes_carrier" FOREIGN KEY ("carrier_id")
          REFERENCES "carriers"("id") ON DELETE CASCADE,
        CONSTRAINT "chk_rate_quotes_base_freight" CHECK ("base_freight" > 0),
        CONSTRAINT "chk_rate_quotes_total_amount" CHECK ("total_amount" > 0),
        CONSTRAINT "chk_rate_quotes_transit_days" CHECK ("transit_days" > 0),
        CONSTRAINT "chk_rate_quotes_availability" CHECK ("availability" >= 0),
        CONSTRAINT "chk_rate_quotes_eta" CHECK ("eta" > "etd"),
        CONSTRAINT "chk_rate_quotes_mode" CHECK ("mode" IN ('FCL', 'LCL'))
      )
    `);

    // Create indexes
    await queryRunner.query(`
      CREATE INDEX "idx_rate_quotes_carrier" ON "rate_quotes" ("carrier_id")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_rate_quotes_origin_dest" ON "rate_quotes" ("origin_code", "destination_code")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_rate_quotes_container_type" ON "rate_quotes" ("container_type")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_rate_quotes_etd" ON "rate_quotes" ("etd")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_rate_quotes_valid_until" ON "rate_quotes" ("valid_until")
    `);
    await queryRunner.query(`
      CREATE INDEX "idx_rate_quotes_created_at" ON "rate_quotes" ("created_at")
    `);

    // Composite index for rate search
    await queryRunner.query(`
      CREATE INDEX "idx_rate_quotes_search" ON "rate_quotes"
        ("origin_code", "destination_code", "container_type", "etd")
    `);

    // Add comments
    await queryRunner.query(`
      COMMENT ON TABLE "rate_quotes" IS 'Shipping rate quotes from carriers (15-min cache)'
    `);
    await queryRunner.query(`
      COMMENT ON COLUMN "rate_quotes"."valid_until" IS 'Quote expiry time (created_at + 15 minutes)'
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`DROP TABLE "rate_quotes"`);
  }
}
