-- Link LedgerEntry rows to their originating GameSession / GameRoom so we can
-- reconcile money flow per round/room. Non-unique because a single session/room
-- produces multiple ledger lines (debit + win/refund).

ALTER TABLE "LedgerEntry" ADD COLUMN "gameSessionId" TEXT;
ALTER TABLE "LedgerEntry" ADD COLUMN "gameRoomId"    TEXT;

CREATE INDEX "LedgerEntry_gameSessionId_idx" ON "LedgerEntry" ("gameSessionId");
CREATE INDEX "LedgerEntry_gameRoomId_idx"    ON "LedgerEntry" ("gameRoomId");

ALTER TABLE "LedgerEntry"
  ADD CONSTRAINT "LedgerEntry_gameSessionId_fkey"
  FOREIGN KEY ("gameSessionId") REFERENCES "GameSession"("id")
  ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE "LedgerEntry"
  ADD CONSTRAINT "LedgerEntry_gameRoomId_fkey"
  FOREIGN KEY ("gameRoomId") REFERENCES "GameRoom"("id")
  ON DELETE SET NULL ON UPDATE CASCADE;

-- Helper indexes for resume + sweep + admin queries.
CREATE INDEX "GameSession_userId_gameType_outcome_idx"
  ON "GameSession" ("userId", "gameType", "outcome");

CREATE INDEX "GameRoom_status_expiresAt_idx" ON "GameRoom" ("status", "expiresAt");
CREATE INDEX "GameRoom_creatorId_status_idx" ON "GameRoom" ("creatorId", "status");
