-- CreateTable
CREATE TABLE "Occupation" (
    "code" TEXT NOT NULL,
    "title" TEXT NOT NULL,
    "description" TEXT,
    "escoUri" TEXT,
    "romeCode" TEXT,
    "crosswalkNote" TEXT,
    "medianWageCents" INTEGER,
    "wageCurrency" TEXT NOT NULL DEFAULT 'USD',
    "employment" INTEGER,

    CONSTRAINT "Occupation_pkey" PRIMARY KEY ("code")
);

-- CreateTable
CREATE TABLE "Task" (
    "id" TEXT NOT NULL,
    "occupationCode" TEXT NOT NULL,
    "statement" TEXT NOT NULL,
    "importance" DOUBLE PRECISION,

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

-- CreateTable
CREATE TABLE "TaskRating" (
    "id" TEXT NOT NULL,
    "taskId" TEXT NOT NULL,
    "dimension" TEXT NOT NULL,
    "model" TEXT NOT NULL,
    "promptVersion" TEXT NOT NULL,
    "sampleIndex" INTEGER NOT NULL DEFAULT 0,
    "rating" INTEGER NOT NULL,
    "rationale" TEXT,
    "rawResponse" JSONB NOT NULL,
    "ratedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "ExpertOverride" (
    "id" TEXT NOT NULL,
    "taskId" TEXT NOT NULL,
    "dimension" TEXT NOT NULL,
    "ratingLow" INTEGER NOT NULL,
    "ratingMid" DOUBLE PRECISION NOT NULL,
    "ratingHigh" INTEGER NOT NULL,
    "reviewer" TEXT NOT NULL,
    "note" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "ScoreRun" (
    "id" TEXT NOT NULL,
    "indexVersion" TEXT NOT NULL,
    "raterPromptVersion" TEXT NOT NULL,
    "raterModels" TEXT[],
    "notes" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "OccupationScore" (
    "id" TEXT NOT NULL,
    "runId" TEXT NOT NULL,
    "occupationCode" TEXT NOT NULL,
    "substitutionLow" DOUBLE PRECISION NOT NULL,
    "substitution" DOUBLE PRECISION NOT NULL,
    "substitutionHigh" DOUBLE PRECISION NOT NULL,
    "exposureLow" DOUBLE PRECISION NOT NULL,
    "exposure" DOUBLE PRECISION NOT NULL,
    "exposureHigh" DOUBLE PRECISION NOT NULL,
    "augmentationLow" DOUBLE PRECISION NOT NULL,
    "augmentation" DOUBLE PRECISION NOT NULL,
    "augmentationHigh" DOUBLE PRECISION NOT NULL,
    "highlyExposedTaskShare" DOUBLE PRECISION NOT NULL,

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

-- CreateTable
CREATE TABLE "TaskScore" (
    "id" TEXT NOT NULL,
    "runId" TEXT NOT NULL,
    "taskId" TEXT NOT NULL,
    "substitutionLow" DOUBLE PRECISION NOT NULL,
    "substitution" DOUBLE PRECISION NOT NULL,
    "substitutionHigh" DOUBLE PRECISION NOT NULL,
    "exposureLow" DOUBLE PRECISION NOT NULL,
    "exposure" DOUBLE PRECISION NOT NULL,
    "exposureHigh" DOUBLE PRECISION NOT NULL,
    "augmentationLow" DOUBLE PRECISION NOT NULL,
    "augmentation" DOUBLE PRECISION NOT NULL,
    "augmentationHigh" DOUBLE PRECISION NOT NULL,

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

-- CreateTable
CREATE TABLE "ApiKey" (
    "id" TEXT NOT NULL,
    "hashedKey" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "rateLimitPerMin" INTEGER NOT NULL DEFAULT 600,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "Task_occupationCode_idx" ON "Task"("occupationCode");

-- CreateIndex
CREATE INDEX "TaskRating_taskId_dimension_idx" ON "TaskRating"("taskId", "dimension");

-- CreateIndex
CREATE INDEX "TaskRating_promptVersion_idx" ON "TaskRating"("promptVersion");

-- CreateIndex
CREATE UNIQUE INDEX "TaskRating_taskId_dimension_model_promptVersion_sampleIndex_key" ON "TaskRating"("taskId", "dimension", "model", "promptVersion", "sampleIndex");

-- CreateIndex
CREATE UNIQUE INDEX "ExpertOverride_taskId_dimension_key" ON "ExpertOverride"("taskId", "dimension");

-- CreateIndex
CREATE INDEX "ScoreRun_indexVersion_idx" ON "ScoreRun"("indexVersion");

-- CreateIndex
CREATE INDEX "OccupationScore_occupationCode_idx" ON "OccupationScore"("occupationCode");

-- CreateIndex
CREATE UNIQUE INDEX "OccupationScore_runId_occupationCode_key" ON "OccupationScore"("runId", "occupationCode");

-- CreateIndex
CREATE INDEX "TaskScore_taskId_idx" ON "TaskScore"("taskId");

-- CreateIndex
CREATE UNIQUE INDEX "TaskScore_runId_taskId_key" ON "TaskScore"("runId", "taskId");

-- CreateIndex
CREATE UNIQUE INDEX "ApiKey_hashedKey_key" ON "ApiKey"("hashedKey");

-- AddForeignKey
ALTER TABLE "Task" ADD CONSTRAINT "Task_occupationCode_fkey" FOREIGN KEY ("occupationCode") REFERENCES "Occupation"("code") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TaskRating" ADD CONSTRAINT "TaskRating_taskId_fkey" FOREIGN KEY ("taskId") REFERENCES "Task"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ExpertOverride" ADD CONSTRAINT "ExpertOverride_taskId_fkey" FOREIGN KEY ("taskId") REFERENCES "Task"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "OccupationScore" ADD CONSTRAINT "OccupationScore_runId_fkey" FOREIGN KEY ("runId") REFERENCES "ScoreRun"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "OccupationScore" ADD CONSTRAINT "OccupationScore_occupationCode_fkey" FOREIGN KEY ("occupationCode") REFERENCES "Occupation"("code") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TaskScore" ADD CONSTRAINT "TaskScore_runId_fkey" FOREIGN KEY ("runId") REFERENCES "ScoreRun"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "TaskScore" ADD CONSTRAINT "TaskScore_taskId_fkey" FOREIGN KEY ("taskId") REFERENCES "Task"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
