-- CreateTable
CREATE TABLE "models" (
    "id" TEXT NOT NULL,
    "slug" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "provider" TEXT NOT NULL,
    "context_length" INTEGER,
    "prompt_price_per_m" DOUBLE PRECISION,
    "completion_price_per_m" DOUBLE PRECISION,
    "active" BOOLEAN NOT NULL DEFAULT true,
    "ranked" BOOLEAN NOT NULL DEFAULT false,
    "synced_at" TIMESTAMP(3) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "eval_items" (
    "id" TEXT NOT NULL,
    "template_id" TEXT NOT NULL,
    "domain" TEXT NOT NULL,
    "prompt" TEXT NOT NULL,
    "answer_key" TEXT NOT NULL,
    "rubric" TEXT,
    "perturb_seed" TEXT,
    "is_anchor" BOOLEAN NOT NULL DEFAULT false,
    "irt_a" DOUBLE PRECISION,
    "irt_b" DOUBLE PRECISION,
    "status" TEXT NOT NULL DEFAULT 'active',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "score_runs" (
    "id" TEXT NOT NULL,
    "index_version" TEXT NOT NULL,
    "kind" TEXT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'pending',
    "item_set_hash" TEXT NOT NULL,
    "model_set" JSONB NOT NULL,
    "fit_diagnostics" JSONB,
    "notes" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completed_at" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "model_responses" (
    "id" TEXT NOT NULL,
    "run_id" TEXT NOT NULL,
    "model_id" TEXT NOT NULL,
    "item_id" TEXT NOT NULL,
    "sample_index" INTEGER NOT NULL DEFAULT 0,
    "request_params" JSONB NOT NULL,
    "raw_response" JSONB NOT NULL,
    "answer_extracted" TEXT,
    "correct" BOOLEAN,
    "confidence" DOUBLE PRECISION,
    "tokens_in" INTEGER,
    "tokens_out" INTEGER,
    "latency_ms" INTEGER,
    "cost_usd" DOUBLE PRECISION,
    "error" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "pairwise_duels" (
    "id" TEXT NOT NULL,
    "run_id" TEXT NOT NULL,
    "domain" TEXT NOT NULL,
    "item_id" TEXT NOT NULL,
    "model_a_id" TEXT NOT NULL,
    "model_b_id" TEXT NOT NULL,
    "judge_slug" TEXT NOT NULL,
    "position_swapped" BOOLEAN NOT NULL DEFAULT false,
    "winner" TEXT NOT NULL,
    "raw_judgment" JSONB NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "scores" (
    "id" TEXT NOT NULL,
    "run_id" TEXT NOT NULL,
    "model_id" TEXT NOT NULL,
    "domain" TEXT NOT NULL,
    "score" DOUBLE PRECISION NOT NULL,
    "score_low" DOUBLE PRECISION NOT NULL,
    "score_high" DOUBLE PRECISION NOT NULL,
    "sub_metrics" JSONB,

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

-- CreateIndex
CREATE UNIQUE INDEX "models_slug_key" ON "models"("slug");

-- CreateIndex
CREATE INDEX "eval_items_domain_status_idx" ON "eval_items"("domain", "status");

-- CreateIndex
CREATE INDEX "score_runs_kind_status_created_at_idx" ON "score_runs"("kind", "status", "created_at");

-- CreateIndex
CREATE INDEX "model_responses_model_id_created_at_idx" ON "model_responses"("model_id", "created_at");

-- CreateIndex
CREATE UNIQUE INDEX "model_responses_run_id_model_id_item_id_sample_index_key" ON "model_responses"("run_id", "model_id", "item_id", "sample_index");

-- CreateIndex
CREATE INDEX "pairwise_duels_run_id_domain_idx" ON "pairwise_duels"("run_id", "domain");

-- CreateIndex
CREATE INDEX "scores_domain_idx" ON "scores"("domain");

-- CreateIndex
CREATE UNIQUE INDEX "scores_run_id_model_id_domain_key" ON "scores"("run_id", "model_id", "domain");

-- AddForeignKey
ALTER TABLE "model_responses" ADD CONSTRAINT "model_responses_run_id_fkey" FOREIGN KEY ("run_id") REFERENCES "score_runs"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "model_responses" ADD CONSTRAINT "model_responses_model_id_fkey" FOREIGN KEY ("model_id") REFERENCES "models"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "model_responses" ADD CONSTRAINT "model_responses_item_id_fkey" FOREIGN KEY ("item_id") REFERENCES "eval_items"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pairwise_duels" ADD CONSTRAINT "pairwise_duels_run_id_fkey" FOREIGN KEY ("run_id") REFERENCES "score_runs"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pairwise_duels" ADD CONSTRAINT "pairwise_duels_item_id_fkey" FOREIGN KEY ("item_id") REFERENCES "eval_items"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pairwise_duels" ADD CONSTRAINT "pairwise_duels_model_a_id_fkey" FOREIGN KEY ("model_a_id") REFERENCES "models"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pairwise_duels" ADD CONSTRAINT "pairwise_duels_model_b_id_fkey" FOREIGN KEY ("model_b_id") REFERENCES "models"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "scores" ADD CONSTRAINT "scores_run_id_fkey" FOREIGN KEY ("run_id") REFERENCES "score_runs"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "scores" ADD CONSTRAINT "scores_model_id_fkey" FOREIGN KEY ("model_id") REFERENCES "models"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

