-- CreateEnum
CREATE TYPE "public"."ExperienceTemplateState" AS ENUM ('draft', 'published', 'archived');

-- CreateEnum
CREATE TYPE "public"."ExperienceFindingPriority" AS ENUM ('low', 'medium', 'high', 'critical');

-- CreateEnum
CREATE TYPE "public"."ExperienceFindingStatus" AS ENUM ('open', 'in_progress', 'resolved', 'closed');

-- CreateEnum
CREATE TYPE "public"."ExperienceScoreAggregationMode" AS ENUM ('weighted_recent', 'moving_average', 'full_average');

-- CreateEnum
CREATE TYPE "public"."ExperienceTrendDirection" AS ENUM ('up', 'down', 'flat');

-- CreateEnum
CREATE TYPE "public"."ExperienceSignalStatus" AS ENUM ('green', 'yellow', 'red');

-- CreateEnum
CREATE TYPE "public"."ExperienceEvidenceKind" AS ENUM ('photo', 'document', 'link');

-- CreateTable
CREATE TABLE "public"."Location" (
    "id" TEXT NOT NULL,
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "city" TEXT,
    "managerUserId" TEXT,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "public"."ExperienceTemplate" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "version" INTEGER NOT NULL,
    "state" "public"."ExperienceTemplateState" NOT NULL DEFAULT 'draft',
    "isDefault" BOOLEAN NOT NULL DEFAULT false,
    "createdById" TEXT,
    "publishedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "public"."ExperienceTemplateCategory" (
    "id" TEXT NOT NULL,
    "templateId" TEXT NOT NULL,
    "key" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "weight" DOUBLE PRECISION NOT NULL DEFAULT 1,
    "sortOrder" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "public"."ExperienceTemplateItem" (
    "id" TEXT NOT NULL,
    "categoryId" TEXT NOT NULL,
    "key" TEXT NOT NULL,
    "label" TEXT NOT NULL,
    "weight" DOUBLE PRECISION NOT NULL DEFAULT 1,
    "sortOrder" INTEGER NOT NULL DEFAULT 0,
    "allowsComment" BOOLEAN NOT NULL DEFAULT true,
    "requiresObservation" BOOLEAN NOT NULL DEFAULT false,
    "allowsEvidence" BOOLEAN NOT NULL DEFAULT true,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "public"."ExperienceEvaluation" (
    "id" TEXT NOT NULL,
    "locationId" TEXT NOT NULL,
    "templateId" TEXT NOT NULL,
    "createdById" TEXT,
    "evaluatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "generalObservations" TEXT NOT NULL DEFAULT '',
    "strengths" TEXT NOT NULL DEFAULT '',
    "improvementAreas" TEXT NOT NULL DEFAULT '',
    "totalScore" DOUBLE PRECISION NOT NULL,
    "signal" "public"."ExperienceSignalStatus" NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "public"."ExperienceEvaluationResponse" (
    "id" TEXT NOT NULL,
    "evaluationId" TEXT NOT NULL,
    "categoryId" TEXT NOT NULL,
    "itemId" TEXT NOT NULL,
    "score" INTEGER NOT NULL,
    "scorePct" DOUBLE PRECISION NOT NULL,
    "comment" TEXT,
    "observation" TEXT,
    "hasObservation" BOOLEAN NOT NULL DEFAULT false,
    "categoryLabelSnapshot" TEXT NOT NULL,
    "itemLabelSnapshot" TEXT NOT NULL,
    "categoryWeightSnapshot" DOUBLE PRECISION NOT NULL,
    "itemWeightSnapshot" DOUBLE PRECISION NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "public"."ExperienceFinding" (
    "id" TEXT NOT NULL,
    "locationId" TEXT NOT NULL,
    "evaluationId" TEXT,
    "createdById" TEXT,
    "responsibleUserId" TEXT,
    "title" TEXT NOT NULL,
    "description" TEXT NOT NULL DEFAULT '',
    "categoryKey" TEXT,
    "categoryLabel" TEXT,
    "priority" "public"."ExperienceFindingPriority" NOT NULL DEFAULT 'medium',
    "status" "public"."ExperienceFindingStatus" NOT NULL DEFAULT 'open',
    "dueDate" TIMESTAMP(3),
    "resolvedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "public"."ExperienceEvidence" (
    "id" TEXT NOT NULL,
    "locationId" TEXT NOT NULL,
    "evaluationId" TEXT,
    "findingId" TEXT,
    "responseId" TEXT,
    "uploadedById" TEXT,
    "kind" "public"."ExperienceEvidenceKind" NOT NULL,
    "title" TEXT NOT NULL,
    "note" TEXT,
    "url" TEXT,
    "storagePath" TEXT,
    "mimeType" TEXT,
    "sizeBytes" INTEGER,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "public"."ExperienceLocationMetric" (
    "id" TEXT NOT NULL,
    "locationId" TEXT NOT NULL,
    "currentScore" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "previousScore" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "trendDelta" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "trendDirection" "public"."ExperienceTrendDirection" NOT NULL DEFAULT 'flat',
    "signal" "public"."ExperienceSignalStatus" NOT NULL DEFAULT 'yellow',
    "totalEvaluations" INTEGER NOT NULL DEFAULT 0,
    "openFindings" INTEGER NOT NULL DEFAULT 0,
    "lastEvaluationAt" TIMESTAMP(3),
    "lastEvaluationId" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "public"."ExperienceScoringPolicy" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "isActive" BOOLEAN NOT NULL DEFAULT true,
    "aggregationMode" "public"."ExperienceScoreAggregationMode" NOT NULL DEFAULT 'weighted_recent',
    "recentWindow" INTEGER NOT NULL DEFAULT 3,
    "recentWeightsCsv" TEXT NOT NULL DEFAULT '0.5,0.3,0.2',
    "greenThreshold" DOUBLE PRECISION NOT NULL DEFAULT 85,
    "yellowThreshold" DOUBLE PRECISION NOT NULL DEFAULT 70,
    "createdById" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "Location_code_key" ON "public"."Location"("code");

-- CreateIndex
CREATE INDEX "Location_isActive_name_idx" ON "public"."Location"("isActive", "name");

-- CreateIndex
CREATE INDEX "ExperienceTemplate_state_updatedAt_idx" ON "public"."ExperienceTemplate"("state", "updatedAt");

-- CreateIndex
CREATE UNIQUE INDEX "ExperienceTemplate_name_version_key" ON "public"."ExperienceTemplate"("name", "version");

-- CreateIndex
CREATE INDEX "ExperienceTemplateCategory_templateId_sortOrder_idx" ON "public"."ExperienceTemplateCategory"("templateId", "sortOrder");

-- CreateIndex
CREATE UNIQUE INDEX "ExperienceTemplateCategory_templateId_key_key" ON "public"."ExperienceTemplateCategory"("templateId", "key");

-- CreateIndex
CREATE INDEX "ExperienceTemplateItem_categoryId_sortOrder_idx" ON "public"."ExperienceTemplateItem"("categoryId", "sortOrder");

-- CreateIndex
CREATE UNIQUE INDEX "ExperienceTemplateItem_categoryId_key_key" ON "public"."ExperienceTemplateItem"("categoryId", "key");

-- CreateIndex
CREATE INDEX "ExperienceEvaluation_locationId_evaluatedAt_idx" ON "public"."ExperienceEvaluation"("locationId", "evaluatedAt");

-- CreateIndex
CREATE INDEX "ExperienceEvaluation_templateId_evaluatedAt_idx" ON "public"."ExperienceEvaluation"("templateId", "evaluatedAt");

-- CreateIndex
CREATE INDEX "ExperienceEvaluationResponse_evaluationId_categoryId_idx" ON "public"."ExperienceEvaluationResponse"("evaluationId", "categoryId");

-- CreateIndex
CREATE UNIQUE INDEX "ExperienceEvaluationResponse_evaluationId_itemId_key" ON "public"."ExperienceEvaluationResponse"("evaluationId", "itemId");

-- CreateIndex
CREATE INDEX "ExperienceFinding_locationId_status_createdAt_idx" ON "public"."ExperienceFinding"("locationId", "status", "createdAt");

-- CreateIndex
CREATE INDEX "ExperienceFinding_evaluationId_idx" ON "public"."ExperienceFinding"("evaluationId");

-- CreateIndex
CREATE INDEX "ExperienceFinding_responsibleUserId_status_idx" ON "public"."ExperienceFinding"("responsibleUserId", "status");

-- CreateIndex
CREATE INDEX "ExperienceEvidence_locationId_createdAt_idx" ON "public"."ExperienceEvidence"("locationId", "createdAt");

-- CreateIndex
CREATE INDEX "ExperienceEvidence_evaluationId_createdAt_idx" ON "public"."ExperienceEvidence"("evaluationId", "createdAt");

-- CreateIndex
CREATE INDEX "ExperienceEvidence_findingId_createdAt_idx" ON "public"."ExperienceEvidence"("findingId", "createdAt");

-- CreateIndex
CREATE UNIQUE INDEX "ExperienceLocationMetric_locationId_key" ON "public"."ExperienceLocationMetric"("locationId");

-- CreateIndex
CREATE INDEX "ExperienceLocationMetric_signal_currentScore_idx" ON "public"."ExperienceLocationMetric"("signal", "currentScore");

-- CreateIndex
CREATE INDEX "ExperienceScoringPolicy_isActive_updatedAt_idx" ON "public"."ExperienceScoringPolicy"("isActive", "updatedAt");

-- AddForeignKey
ALTER TABLE "public"."Location" ADD CONSTRAINT "Location_managerUserId_fkey" FOREIGN KEY ("managerUserId") REFERENCES "public"."User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceTemplate" ADD CONSTRAINT "ExperienceTemplate_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "public"."User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceTemplateCategory" ADD CONSTRAINT "ExperienceTemplateCategory_templateId_fkey" FOREIGN KEY ("templateId") REFERENCES "public"."ExperienceTemplate"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceTemplateItem" ADD CONSTRAINT "ExperienceTemplateItem_categoryId_fkey" FOREIGN KEY ("categoryId") REFERENCES "public"."ExperienceTemplateCategory"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceEvaluation" ADD CONSTRAINT "ExperienceEvaluation_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "public"."Location"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceEvaluation" ADD CONSTRAINT "ExperienceEvaluation_templateId_fkey" FOREIGN KEY ("templateId") REFERENCES "public"."ExperienceTemplate"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceEvaluation" ADD CONSTRAINT "ExperienceEvaluation_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "public"."User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceEvaluationResponse" ADD CONSTRAINT "ExperienceEvaluationResponse_evaluationId_fkey" FOREIGN KEY ("evaluationId") REFERENCES "public"."ExperienceEvaluation"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceEvaluationResponse" ADD CONSTRAINT "ExperienceEvaluationResponse_categoryId_fkey" FOREIGN KEY ("categoryId") REFERENCES "public"."ExperienceTemplateCategory"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceEvaluationResponse" ADD CONSTRAINT "ExperienceEvaluationResponse_itemId_fkey" FOREIGN KEY ("itemId") REFERENCES "public"."ExperienceTemplateItem"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceFinding" ADD CONSTRAINT "ExperienceFinding_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "public"."Location"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceFinding" ADD CONSTRAINT "ExperienceFinding_evaluationId_fkey" FOREIGN KEY ("evaluationId") REFERENCES "public"."ExperienceEvaluation"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceFinding" ADD CONSTRAINT "ExperienceFinding_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "public"."User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceFinding" ADD CONSTRAINT "ExperienceFinding_responsibleUserId_fkey" FOREIGN KEY ("responsibleUserId") REFERENCES "public"."User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceEvidence" ADD CONSTRAINT "ExperienceEvidence_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "public"."Location"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceEvidence" ADD CONSTRAINT "ExperienceEvidence_evaluationId_fkey" FOREIGN KEY ("evaluationId") REFERENCES "public"."ExperienceEvaluation"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceEvidence" ADD CONSTRAINT "ExperienceEvidence_findingId_fkey" FOREIGN KEY ("findingId") REFERENCES "public"."ExperienceFinding"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceEvidence" ADD CONSTRAINT "ExperienceEvidence_responseId_fkey" FOREIGN KEY ("responseId") REFERENCES "public"."ExperienceEvaluationResponse"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceEvidence" ADD CONSTRAINT "ExperienceEvidence_uploadedById_fkey" FOREIGN KEY ("uploadedById") REFERENCES "public"."User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceLocationMetric" ADD CONSTRAINT "ExperienceLocationMetric_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "public"."Location"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceLocationMetric" ADD CONSTRAINT "ExperienceLocationMetric_lastEvaluationId_fkey" FOREIGN KEY ("lastEvaluationId") REFERENCES "public"."ExperienceEvaluation"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."ExperienceScoringPolicy" ADD CONSTRAINT "ExperienceScoringPolicy_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "public"."User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
