-- CreateTable
CREATE TABLE "users" (
    "id" UUID NOT NULL,
    "twitch_opaque_user_id" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "survivors" (
    "id" UUID NOT NULL,
    "user_id" UUID NOT NULL,
    "channel_id" TEXT NOT NULL,
    "name" VARCHAR(32) NOT NULL,
    "state" TEXT NOT NULL DEFAULT 'active',
    "stats" JSONB NOT NULL,
    "perk_slots" JSONB NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "missions" (
    "id" UUID NOT NULL,
    "group_id" UUID,
    "channel_id" TEXT NOT NULL,
    "difficulty" SMALLINT NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'active',
    "encounter_library_version" TEXT NOT NULL,
    "started_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "ended_at" TIMESTAMP(3),
    "tick_index" INTEGER NOT NULL DEFAULT 0,
    "next_tick_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "mission_participants" (
    "id" UUID NOT NULL,
    "mission_id" UUID NOT NULL,
    "survivor_id" UUID NOT NULL,
    "state" TEXT NOT NULL DEFAULT 'active',
    "hook_count" SMALLINT NOT NULL DEFAULT 0,

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

-- CreateTable
CREATE TABLE "mission_logs" (
    "id" UUID NOT NULL,
    "mission_id" UUID NOT NULL,
    "tick_index" INTEGER NOT NULL,
    "encounter_key" TEXT NOT NULL,
    "rendered_text" TEXT NOT NULL,
    "seed" TEXT NOT NULL,
    "modifiers_applied" JSONB NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "users_twitch_opaque_user_id_key" ON "users"("twitch_opaque_user_id");

-- CreateIndex
CREATE INDEX "missions_channel_id_idx" ON "missions"("channel_id");

-- CreateIndex
CREATE INDEX "missions_status_next_tick_at_idx" ON "missions"("status", "next_tick_at");

-- CreateIndex
CREATE UNIQUE INDEX "mission_participants_mission_id_survivor_id_key" ON "mission_participants"("mission_id", "survivor_id");

-- CreateIndex
CREATE INDEX "mission_logs_mission_id_tick_index_idx" ON "mission_logs"("mission_id", "tick_index");

-- AddForeignKey
ALTER TABLE "survivors" ADD CONSTRAINT "survivors_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mission_participants" ADD CONSTRAINT "mission_participants_mission_id_fkey" FOREIGN KEY ("mission_id") REFERENCES "missions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mission_participants" ADD CONSTRAINT "mission_participants_survivor_id_fkey" FOREIGN KEY ("survivor_id") REFERENCES "survivors"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mission_logs" ADD CONSTRAINT "mission_logs_mission_id_fkey" FOREIGN KEY ("mission_id") REFERENCES "missions"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

