-- CreateEnum
CREATE TYPE "public"."CaptureMode" AS ENUM ('manual', 'auto', 'hybrid');

-- CreateEnum
CREATE TYPE "public"."CaptureAutomationRunStatus" AS ENUM ('queued', 'running', 'success', 'failed');

-- AlterTable
ALTER TABLE "public"."WeeklyKpiRow"
ADD COLUMN "captureNote" TEXT,
ADD COLUMN "lastAutomationAt" TIMESTAMP(3),
ADD COLUMN "lastCapturedAt" TIMESTAMP(3),
ADD COLUMN "lastCapturedById" TEXT;

-- CreateTable
CREATE TABLE "public"."KpiCaptureCatalog" (
    "id" TEXT NOT NULL,
    "weekStart" TIMESTAMP(3) NOT NULL,
    "sectionKey" TEXT NOT NULL,
    "rowKey" TEXT NOT NULL,
    "weeklyKpiRowId" TEXT,
    "department" "public"."DepartmentKey" NOT NULL,
    "captureMode" "public"."CaptureMode" NOT NULL DEFAULT 'manual',
    "automationSource" TEXT,
    "ownerUserId" TEXT,
    "freshnessSlaHours" INTEGER NOT NULL DEFAULT 168,
    "captureNote" TEXT,
    "lastCapturedAt" TIMESTAMP(3),
    "lastCapturedById" TEXT,
    "lastAutomationAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "public"."KpiCaptureEvidence" (
    "id" TEXT NOT NULL,
    "department" "public"."DepartmentKey" NOT NULL,
    "weekStart" TIMESTAMP(3) NOT NULL,
    "sectionKey" TEXT NOT NULL,
    "rowKey" TEXT NOT NULL,
    "catalogId" TEXT,
    "weeklyKpiRowId" TEXT,
    "initiativeId" TEXT,
    "taskId" TEXT,
    "uploadedById" TEXT NOT NULL,
    "kind" "public"."ProjectCaptureEvidenceKind" 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 "KpiCaptureEvidence_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "public"."KpiCaptureAutomationRun" (
    "id" TEXT NOT NULL,
    "department" "public"."DepartmentKey" NOT NULL,
    "weekStart" TIMESTAMP(3) NOT NULL,
    "source" TEXT NOT NULL,
    "status" "public"."CaptureAutomationRunStatus" NOT NULL DEFAULT 'queued',
    "rowsTouched" INTEGER NOT NULL DEFAULT 0,
    "errorMessage" TEXT,
    "triggeredById" TEXT,
    "startedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completedAt" TIMESTAMP(3),
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "KpiCaptureCatalog_weekStart_sectionKey_rowKey_key" ON "public"."KpiCaptureCatalog"("weekStart", "sectionKey", "rowKey");

-- CreateIndex
CREATE UNIQUE INDEX "KpiCaptureCatalog_weeklyKpiRowId_key" ON "public"."KpiCaptureCatalog"("weeklyKpiRowId");

-- CreateIndex
CREATE INDEX "KpiCaptureCatalog_department_weekStart_idx" ON "public"."KpiCaptureCatalog"("department", "weekStart");

-- CreateIndex
CREATE INDEX "KpiCaptureCatalog_ownerUserId_weekStart_idx" ON "public"."KpiCaptureCatalog"("ownerUserId", "weekStart");

-- CreateIndex
CREATE INDEX "KpiCaptureEvidence_department_weekStart_createdAt_idx" ON "public"."KpiCaptureEvidence"("department", "weekStart", "createdAt");

-- CreateIndex
CREATE INDEX "KpiCaptureEvidence_sectionKey_rowKey_weekStart_idx" ON "public"."KpiCaptureEvidence"("sectionKey", "rowKey", "weekStart");

-- CreateIndex
CREATE INDEX "KpiCaptureEvidence_catalogId_createdAt_idx" ON "public"."KpiCaptureEvidence"("catalogId", "createdAt");

-- CreateIndex
CREATE INDEX "KpiCaptureEvidence_initiativeId_createdAt_idx" ON "public"."KpiCaptureEvidence"("initiativeId", "createdAt");

-- CreateIndex
CREATE INDEX "KpiCaptureEvidence_taskId_createdAt_idx" ON "public"."KpiCaptureEvidence"("taskId", "createdAt");

-- CreateIndex
CREATE INDEX "KpiCaptureEvidence_uploadedById_createdAt_idx" ON "public"."KpiCaptureEvidence"("uploadedById", "createdAt");

-- CreateIndex
CREATE INDEX "KpiCaptureAutomationRun_department_weekStart_createdAt_idx" ON "public"."KpiCaptureAutomationRun"("department", "weekStart", "createdAt");

-- CreateIndex
CREATE INDEX "KpiCaptureAutomationRun_status_createdAt_idx" ON "public"."KpiCaptureAutomationRun"("status", "createdAt");

-- AddForeignKey
ALTER TABLE "public"."KpiCaptureCatalog"
ADD CONSTRAINT "KpiCaptureCatalog_weeklyKpiRowId_fkey"
FOREIGN KEY ("weeklyKpiRowId") REFERENCES "public"."WeeklyKpiRow"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."KpiCaptureCatalog"
ADD CONSTRAINT "KpiCaptureCatalog_ownerUserId_fkey"
FOREIGN KEY ("ownerUserId") REFERENCES "public"."User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."KpiCaptureCatalog"
ADD CONSTRAINT "KpiCaptureCatalog_lastCapturedById_fkey"
FOREIGN KEY ("lastCapturedById") REFERENCES "public"."User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."KpiCaptureEvidence"
ADD CONSTRAINT "KpiCaptureEvidence_catalogId_fkey"
FOREIGN KEY ("catalogId") REFERENCES "public"."KpiCaptureCatalog"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."KpiCaptureEvidence"
ADD CONSTRAINT "KpiCaptureEvidence_weeklyKpiRowId_fkey"
FOREIGN KEY ("weeklyKpiRowId") REFERENCES "public"."WeeklyKpiRow"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."KpiCaptureEvidence"
ADD CONSTRAINT "KpiCaptureEvidence_initiativeId_fkey"
FOREIGN KEY ("initiativeId") REFERENCES "public"."MarketingInitiative"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."KpiCaptureEvidence"
ADD CONSTRAINT "KpiCaptureEvidence_taskId_fkey"
FOREIGN KEY ("taskId") REFERENCES "public"."MarketingTask"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."KpiCaptureEvidence"
ADD CONSTRAINT "KpiCaptureEvidence_uploadedById_fkey"
FOREIGN KEY ("uploadedById") REFERENCES "public"."User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "public"."KpiCaptureAutomationRun"
ADD CONSTRAINT "KpiCaptureAutomationRun_triggeredById_fkey"
FOREIGN KEY ("triggeredById") REFERENCES "public"."User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
