-- Sprint-niveau jobflow met cascade-FAIL (PBI-46 / F1).
-- Voegt FAILED toe aan TaskStatus, StoryStatus, PbiStatus, SprintStatus.
-- Introduceert SprintRunStatus en PrStrategy enums.
-- Maakt sprint_runs tabel + ClaudeJob.sprint_run_id koppeling + Product.pr_strategy.
--
-- Gegenereerd via: npx prisma migrate diff --from-config-datasource --to-schema prisma/schema.prisma
-- (handmatig opgeschoond: todos-tabel wijzigingen weggelaten — zit in een separate migratie #131).

-- CreateEnum
CREATE TYPE "SprintRunStatus" AS ENUM ('QUEUED', 'RUNNING', 'PAUSED', 'DONE', 'FAILED', 'CANCELLED');

-- CreateEnum
CREATE TYPE "PrStrategy" AS ENUM ('SPRINT', 'STORY');

-- AlterEnum
ALTER TYPE "PbiStatus" ADD VALUE 'FAILED';

-- AlterEnum
ALTER TYPE "SprintStatus" ADD VALUE 'FAILED';

-- AlterEnum
ALTER TYPE "StoryStatus" ADD VALUE 'FAILED';

-- AlterEnum
ALTER TYPE "TaskStatus" ADD VALUE 'FAILED';

-- AlterTable
ALTER TABLE "claude_jobs" ADD COLUMN     "sprint_run_id" TEXT;

-- AlterTable
ALTER TABLE "products" ADD COLUMN     "pr_strategy" "PrStrategy" NOT NULL DEFAULT 'SPRINT';

-- CreateTable
CREATE TABLE "sprint_runs" (
    "id" TEXT NOT NULL,
    "sprint_id" TEXT NOT NULL,
    "started_by_id" TEXT NOT NULL,
    "status" "SprintRunStatus" NOT NULL DEFAULT 'QUEUED',
    "pr_strategy" "PrStrategy" NOT NULL,
    "branch" TEXT,
    "pr_url" TEXT,
    "started_at" TIMESTAMP(3),
    "finished_at" TIMESTAMP(3),
    "failure_reason" TEXT,
    "failed_task_id" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE INDEX "sprint_runs_sprint_id_status_idx" ON "sprint_runs"("sprint_id", "status");

-- CreateIndex
CREATE INDEX "sprint_runs_started_by_id_status_idx" ON "sprint_runs"("started_by_id", "status");

-- CreateIndex
CREATE INDEX "claude_jobs_sprint_run_id_status_idx" ON "claude_jobs"("sprint_run_id", "status");

-- AddForeignKey
ALTER TABLE "sprint_runs" ADD CONSTRAINT "sprint_runs_sprint_id_fkey" FOREIGN KEY ("sprint_id") REFERENCES "sprints"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sprint_runs" ADD CONSTRAINT "sprint_runs_started_by_id_fkey" FOREIGN KEY ("started_by_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "sprint_runs" ADD CONSTRAINT "sprint_runs_failed_task_id_fkey" FOREIGN KEY ("failed_task_id") REFERENCES "tasks"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "claude_jobs" ADD CONSTRAINT "claude_jobs_sprint_run_id_fkey" FOREIGN KEY ("sprint_run_id") REFERENCES "sprint_runs"("id") ON DELETE SET NULL ON UPDATE CASCADE;
