-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "public";

-- CreateTable
CREATE TABLE "orders_tariff_reference" (
    "id" SERIAL NOT NULL,
    "uuid" TEXT NOT NULL,
    "team_uuid" VARCHAR(100) NOT NULL,
    "name" VARCHAR(255) NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'active',
    "operation_code" VARCHAR(50),
    "incoterms_code" VARCHAR(20),
    "transport_type_code" VARCHAR(50),
    "tare_type_code" VARCHAR(50),
    "source_country_code" VARCHAR(10),
    "destination_country_code" VARCHAR(10),
    "source_hub_uuid" VARCHAR(100),
    "destination_hub_uuid" VARCHAR(100),
    "min_weight_kg" DECIMAL(12,2),
    "max_weight_kg" DECIMAL(12,2),
    "min_volume_cbm" DECIMAL(12,3),
    "max_volume_cbm" DECIMAL(12,3),
    "min_distance_km" INTEGER,
    "max_distance_km" INTEGER,
    "amount_usd" DECIMAL(12,2) NOT NULL,
    "min_price_usd" DECIMAL(12,2),
    "eta_days" INTEGER,
    "priority" INTEGER NOT NULL DEFAULT 100,
    "currency" VARCHAR(10) NOT NULL DEFAULT 'USD',
    "dmn_expression" TEXT,
    "notes" TEXT,
    "created_by_user_id" VARCHAR(255),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "orders_tariff_reference_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "orders_quotation" (
    "id" SERIAL NOT NULL,
    "uuid" TEXT NOT NULL,
    "team_uuid" VARCHAR(100) NOT NULL,
    "created_by_user_id" VARCHAR(255),
    "title" VARCHAR(255) NOT NULL DEFAULT 'Quotation',
    "status" VARCHAR(30) NOT NULL DEFAULT 'draft',
    "operation_code" VARCHAR(50),
    "incoterms_code" VARCHAR(20),
    "transport_type_code" VARCHAR(50),
    "tare_type_code" VARCHAR(50),
    "source_country_code" VARCHAR(10),
    "source_hub_uuid" VARCHAR(100),
    "source_location_uuid" VARCHAR(100),
    "source_location_name" VARCHAR(255),
    "source_latitude" DOUBLE PRECISION,
    "source_longitude" DOUBLE PRECISION,
    "destination_country_code" VARCHAR(10),
    "destination_hub_uuid" VARCHAR(100),
    "destination_location_uuid" VARCHAR(100),
    "destination_location_name" VARCHAR(255),
    "destination_latitude" DOUBLE PRECISION,
    "destination_longitude" DOUBLE PRECISION,
    "chargeable_weight_kg" DECIMAL(12,2),
    "gross_weight_kg" DECIMAL(12,2),
    "volume_cbm" DECIMAL(12,3),
    "units_count" INTEGER,
    "route_distance_km" INTEGER,
    "selected_tariff_id" INTEGER,
    "tariff_match_summary" TEXT,
    "tariff_snapshot" TEXT,
    "total_amount" DECIMAL(12,2) NOT NULL DEFAULT 0,
    "currency" VARCHAR(10) NOT NULL DEFAULT 'USD',
    "eta_days" INTEGER,
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "orders_quotation_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "orders_quotation_change" (
    "id" SERIAL NOT NULL,
    "uuid" TEXT NOT NULL,
    "quotation_id" INTEGER NOT NULL,
    "actor_user_id" VARCHAR(255),
    "actor_label" VARCHAR(255),
    "source" VARCHAR(50) NOT NULL,
    "summary" TEXT,
    "payload_json" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "orders_quotation_change_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "orders_order" (
    "id" SERIAL NOT NULL,
    "uuid" TEXT NOT NULL,
    "team_uuid" VARCHAR(100) NOT NULL,
    "quotation_id" INTEGER,
    "created_by_user_id" VARCHAR(255),
    "name" VARCHAR(255) NOT NULL,
    "status" VARCHAR(30) NOT NULL DEFAULT 'draft',
    "total_amount" DECIMAL(12,2) NOT NULL DEFAULT 0,
    "currency" VARCHAR(10) NOT NULL DEFAULT 'USD',
    "source_location_uuid" VARCHAR(100),
    "source_location_name" VARCHAR(255),
    "source_country_code" VARCHAR(10),
    "source_latitude" DOUBLE PRECISION,
    "source_longitude" DOUBLE PRECISION,
    "destination_location_uuid" VARCHAR(100),
    "destination_location_name" VARCHAR(255),
    "destination_country_code" VARCHAR(10),
    "destination_latitude" DOUBLE PRECISION,
    "destination_longitude" DOUBLE PRECISION,
    "eta_days" INTEGER,
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "orders_order_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "orders_tariff_reference_uuid_key" ON "orders_tariff_reference"("uuid");

-- CreateIndex
CREATE INDEX "orders_tariff_reference_team_uuid_status_priority_idx" ON "orders_tariff_reference"("team_uuid", "status", "priority");

-- CreateIndex
CREATE INDEX "orders_tariff_reference_team_uuid_source_country_code_desti_idx" ON "orders_tariff_reference"("team_uuid", "source_country_code", "destination_country_code");

-- CreateIndex
CREATE UNIQUE INDEX "orders_quotation_uuid_key" ON "orders_quotation"("uuid");

-- CreateIndex
CREATE INDEX "orders_quotation_team_uuid_status_created_at_idx" ON "orders_quotation"("team_uuid", "status", "created_at");

-- CreateIndex
CREATE INDEX "orders_quotation_team_uuid_source_country_code_destination__idx" ON "orders_quotation"("team_uuid", "source_country_code", "destination_country_code");

-- CreateIndex
CREATE UNIQUE INDEX "orders_quotation_change_uuid_key" ON "orders_quotation_change"("uuid");

-- CreateIndex
CREATE INDEX "orders_quotation_change_quotation_id_created_at_idx" ON "orders_quotation_change"("quotation_id", "created_at");

-- CreateIndex
CREATE UNIQUE INDEX "orders_order_uuid_key" ON "orders_order"("uuid");

-- CreateIndex
CREATE UNIQUE INDEX "orders_order_quotation_id_key" ON "orders_order"("quotation_id");

-- CreateIndex
CREATE INDEX "orders_order_team_uuid_created_at_idx" ON "orders_order"("team_uuid", "created_at");

-- AddForeignKey
ALTER TABLE "orders_quotation" ADD CONSTRAINT "orders_quotation_selected_tariff_id_fkey" FOREIGN KEY ("selected_tariff_id") REFERENCES "orders_tariff_reference"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "orders_quotation_change" ADD CONSTRAINT "orders_quotation_change_quotation_id_fkey" FOREIGN KEY ("quotation_id") REFERENCES "orders_quotation"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "orders_order" ADD CONSTRAINT "orders_order_quotation_id_fkey" FOREIGN KEY ("quotation_id") REFERENCES "orders_quotation"("id") ON DELETE SET NULL ON UPDATE CASCADE;

