CREATE TYPE "VehiclePricingMode" AS ENUM ('MANUAL', 'AUTOMATIC');

CREATE TYPE "VehiclePricingRuleType" AS ENUM (
  'DATE_RANGE',
  'SEASONAL',
  'HOLIDAY',
  'WEEKEND',
  'SPECIAL_EVENT',
  'MANUAL_OVERRIDE'
);

CREATE TYPE "VehiclePriceChangeSource" AS ENUM (
  'CONFIG_UPDATE',
  'RULE_CREATED',
  'RULE_UPDATED',
  'RULE_DELETED',
  'MANUAL_OVERRIDE'
);

CREATE TABLE "vehicle_pricing_configurations" (
  "id" TEXT NOT NULL,
  "vehicleId" TEXT NOT NULL,
  "pricingMode" "VehiclePricingMode" NOT NULL DEFAULT 'MANUAL',
  "baseDailyRate" INTEGER,
  "weeklyRate" INTEGER,
  "weekendRate" INTEGER,
  "holidayRate" INTEGER,
  "monthlyRate" INTEGER,
  "longTermDailyRate" INTEGER,
  "minimumDailyRate" INTEGER,
  "maximumDailyRate" INTEGER,
  "maxDailyPriceMovementPct" INTEGER NOT NULL DEFAULT 10,
  "automaticPricingEnabled" BOOLEAN NOT NULL DEFAULT false,
  "approvalRequired" BOOLEAN NOT NULL DEFAULT false,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "vehicle_pricing_configurations_pkey" PRIMARY KEY ("id")
);

CREATE UNIQUE INDEX "vehicle_pricing_configurations_vehicleId_key"
  ON "vehicle_pricing_configurations"("vehicleId");

CREATE TABLE "vehicle_pricing_rules" (
  "id" TEXT NOT NULL,
  "configurationId" TEXT NOT NULL,
  "name" TEXT NOT NULL,
  "ruleType" "VehiclePricingRuleType" NOT NULL DEFAULT 'DATE_RANGE',
  "startDate" TIMESTAMP(3) NOT NULL,
  "endDate" TIMESTAMP(3) NOT NULL,
  "dailyRate" INTEGER,
  "weeklyRate" INTEGER,
  "weekendRate" INTEGER,
  "monthlyRate" INTEGER,
  "minDailyRate" INTEGER,
  "maxDailyRate" INTEGER,
  "automaticAdjustmentPct" INTEGER,
  "priceAdjustment" INTEGER,
  "isActive" BOOLEAN NOT NULL DEFAULT true,
  "sortOrder" INTEGER NOT NULL DEFAULT 0,
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "vehicle_pricing_rules_pkey" PRIMARY KEY ("id")
);

CREATE INDEX "vehicle_pricing_rules_configurationId_idx"
  ON "vehicle_pricing_rules"("configurationId");

CREATE INDEX "vehicle_pricing_rules_configurationId_startDate_endDate_idx"
  ON "vehicle_pricing_rules"("configurationId", "startDate", "endDate");

CREATE TABLE "vehicle_price_history" (
  "id" TEXT NOT NULL,
  "configurationId" TEXT NOT NULL,
  "vehicleId" TEXT NOT NULL,
  "source" "VehiclePriceChangeSource" NOT NULL DEFAULT 'CONFIG_UPDATE',
  "changedByEmployeeId" TEXT,
  "previousDailyRate" INTEGER,
  "nextDailyRate" INTEGER,
  "previousWeeklyRate" INTEGER,
  "nextWeeklyRate" INTEGER,
  "note" TEXT,
  "effectiveFrom" TIMESTAMP(3),
  "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT "vehicle_price_history_pkey" PRIMARY KEY ("id")
);

CREATE INDEX "vehicle_price_history_vehicleId_createdAt_idx"
  ON "vehicle_price_history"("vehicleId", "createdAt");

CREATE INDEX "vehicle_price_history_configurationId_createdAt_idx"
  ON "vehicle_price_history"("configurationId", "createdAt");

ALTER TABLE "vehicle_pricing_configurations"
  ADD CONSTRAINT "vehicle_pricing_configurations_vehicleId_fkey"
  FOREIGN KEY ("vehicleId") REFERENCES "vehicles"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "vehicle_pricing_rules"
  ADD CONSTRAINT "vehicle_pricing_rules_configurationId_fkey"
  FOREIGN KEY ("configurationId") REFERENCES "vehicle_pricing_configurations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "vehicle_price_history"
  ADD CONSTRAINT "vehicle_price_history_configurationId_fkey"
  FOREIGN KEY ("configurationId") REFERENCES "vehicle_pricing_configurations"("id") ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE "vehicle_price_history"
  ADD CONSTRAINT "vehicle_price_history_vehicleId_fkey"
  FOREIGN KEY ("vehicleId") REFERENCES "vehicles"("id") ON DELETE CASCADE ON UPDATE CASCADE;
