-- CreateEnum: FeedbackCategory
CREATE TYPE "FeedbackCategory" AS ENUM (
  'BOOKING',
  'PICKUP',
  'VEHICLE_CLEANLINESS',
  'VEHICLE_CONDITION',
  'STAFF_SERVICE',
  'PRICING',
  'DEPOSIT',
  'INSURANCE',
  'DAMAGE_CLAIM',
  'RETURN_PROCESS',
  'COMMUNICATION',
  'ROADSIDE_ASSISTANCE',
  'BILLING',
  'OTHER'
);

-- CreateEnum: ComplaintSeverity
CREATE TYPE "ComplaintSeverity" AS ENUM ('LEVEL_1', 'LEVEL_2', 'LEVEL_3');

-- CreateEnum: ComplaintStatus
CREATE TYPE "ComplaintStatus" AS ENUM ('OPEN', 'INVESTIGATING', 'RESOLVED', 'CLOSED');

-- AlterTable: reviews — add category column
ALTER TABLE "reviews" ADD COLUMN "category" "FeedbackCategory";

-- AlterTable: reservations — add review tracking columns
ALTER TABLE "reservations"
  ADD COLUMN "reviewRequestSentAt"       TIMESTAMP(3),
  ADD COLUMN "reviewReminderSentAt"      TIMESTAMP(3),
  ADD COLUMN "reviewFinalReminderSentAt" TIMESTAMP(3),
  ADD COLUMN "reviewPaused"              BOOLEAN NOT NULL DEFAULT false;

-- AlterTable: customers — add reviewOptOut column
ALTER TABLE "customers" ADD COLUMN "reviewOptOut" BOOLEAN NOT NULL DEFAULT false;

-- CreateTable: complaints
CREATE TABLE "complaints" (
  "id"            TEXT NOT NULL,
  "companyId"     TEXT NOT NULL,
  "reservationId" TEXT,
  "reviewId"      TEXT,
  "customerId"    TEXT,
  "severity"      "ComplaintSeverity" NOT NULL DEFAULT 'LEVEL_1',
  "status"        "ComplaintStatus"   NOT NULL DEFAULT 'OPEN',
  "category"      "FeedbackCategory"  NOT NULL,
  "subject"       TEXT NOT NULL,
  "description"   TEXT,
  "resolution"    TEXT,
  "notes"         TEXT,
  "assignedTo"    TEXT,
  "resolvedAt"    TIMESTAMP(3),
  "resolvedBy"    TEXT,
  "createdAt"     TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  "updatedAt"     TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE INDEX "complaints_companyId_idx" ON "complaints"("companyId");
CREATE INDEX "complaints_reservationId_idx" ON "complaints"("reservationId");

-- AddForeignKey: complaints → companies
ALTER TABLE "complaints"
  ADD CONSTRAINT "complaints_companyId_fkey"
  FOREIGN KEY ("companyId") REFERENCES "companies"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey: complaints → reservations
ALTER TABLE "complaints"
  ADD CONSTRAINT "complaints_reservationId_fkey"
  FOREIGN KEY ("reservationId") REFERENCES "reservations"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey: complaints → reviews
ALTER TABLE "complaints"
  ADD CONSTRAINT "complaints_reviewId_fkey"
  FOREIGN KEY ("reviewId") REFERENCES "reviews"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey: complaints → customers
ALTER TABLE "complaints"
  ADD CONSTRAINT "complaints_customerId_fkey"
  FOREIGN KEY ("customerId") REFERENCES "customers"("id") ON DELETE SET NULL ON UPDATE CASCADE;
