-- AlterEnum
ALTER TYPE "LedgerType" ADD VALUE 'DEBIT_GAME_BET';
ALTER TYPE "LedgerType" ADD VALUE 'CREDIT_GAME_WIN';
ALTER TYPE "LedgerType" ADD VALUE 'CREDIT_GAME_REFUND';

-- CreateEnum
CREATE TYPE "GameType" AS ENUM ('CRASH', 'DICE', 'MINES', 'TOWER', 'SLOTS', 'BLACKJACK', 'ROULETTE', 'COIN_FLIP', 'PONG', 'PREDICTION');

-- CreateEnum
CREATE TYPE "RoomStatus" AS ENUM ('WAITING', 'ACTIVE', 'RESOLVED', 'EXPIRED');

-- CreateTable
CREATE TABLE "GameSession" (
    "id" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "gameType" "GameType" NOT NULL,
    "wageredBLW" INTEGER NOT NULL,
    "payoutBLW" INTEGER NOT NULL DEFAULT 0,
    "multiplier" DOUBLE PRECISION NOT NULL DEFAULT 0,
    "outcome" TEXT NOT NULL,
    "serverSeed" TEXT NOT NULL,
    "clientSeed" TEXT,
    "resultData" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "GameSession_userId_idx" ON "GameSession"("userId");

-- CreateIndex
CREATE INDEX "GameSession_userId_gameType_idx" ON "GameSession"("userId", "gameType");

-- AddForeignKey
ALTER TABLE "GameSession" ADD CONSTRAINT "GameSession_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- CreateTable
CREATE TABLE "GameRoom" (
    "id" TEXT NOT NULL,
    "gameType" "GameType" NOT NULL,
    "creatorId" TEXT NOT NULL,
    "joinerId" TEXT,
    "wageBLW" INTEGER NOT NULL,
    "status" "RoomStatus" NOT NULL DEFAULT 'WAITING',
    "resultData" JSONB,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expiresAt" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE INDEX "GameRoom_status_idx" ON "GameRoom"("status");

-- CreateTable
CREATE TABLE "PredictionMarket" (
    "id" TEXT NOT NULL,
    "creatorId" TEXT NOT NULL,
    "question" TEXT NOT NULL,
    "endsAt" TIMESTAMP(3) NOT NULL,
    "resolvedTo" BOOLEAN,
    "totalYes" INTEGER NOT NULL DEFAULT 0,
    "totalNo" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "PredictionMarket_endsAt_idx" ON "PredictionMarket"("endsAt");

-- CreateTable
CREATE TABLE "PredictionBet" (
    "id" TEXT NOT NULL,
    "marketId" TEXT NOT NULL,
    "userId" TEXT NOT NULL,
    "side" BOOLEAN NOT NULL,
    "blwAmount" INTEGER NOT NULL,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "PredictionBet_marketId_idx" ON "PredictionBet"("marketId");

-- CreateIndex
CREATE INDEX "PredictionBet_userId_idx" ON "PredictionBet"("userId");

-- AddForeignKey
ALTER TABLE "PredictionBet" ADD CONSTRAINT "PredictionBet_marketId_fkey" FOREIGN KEY ("marketId") REFERENCES "PredictionMarket"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "PredictionBet" ADD CONSTRAINT "PredictionBet_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
