generator client {
  provider = "prisma-client"
  output   = "../lib/generated/prisma"
}

datasource db {
  provider = "postgresql"
}

model User {
  id           String        @id @default(uuid(7))
  name         String
  email        String        @unique
  password     String
  banned       Boolean       @default(false)
  createdAt    DateTime      @default(now())
  games        Game[]
  dailyResults DailyResult[]
}

model Game {
  id            String   @id @default(uuid(7))
  userId        String
  user          User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  mode          String // "solo" | "multi" | "daily" | "blitz"
  startArticle  String
  targetArticle String
  path          String // JSON array de titres
  clicks        Int
  timeSeconds   Float
  won           Boolean  @default(true)
  playedAt      DateTime @default(now())

  @@index([userId])
}

model DailyPuzzle {
  id            String        @id @default(uuid(7))
  date          String        @unique // "YYYY-MM-DD"
  startArticle  String
  targetArticle String
  results       DailyResult[]
}

model Room {
  code          String   @id
  players       Json     @default("[]")
  phase         String   @default("waiting")
  round         Int      @default(0)
  totalRounds   Int      @default(3)
  maxPlayers    Int      @default(16)
  gameMode      String   @default("race")
  startArticle  String   @default("")
  targetArticle String   @default("")
  roundWinner   String?
  countdownStart BigInt?
  roundStart    BigInt?
  createdAt     BigInt
  updatedAt     DateTime @updatedAt
}

model DailyResult {
  id          String      @id @default(uuid(7))
  puzzleId    String
  puzzle      DailyPuzzle @relation(fields: [puzzleId], references: [id], onDelete: Cascade)
  userId      String
  user        User        @relation(fields: [userId], references: [id], onDelete: Cascade)
  path        String // JSON
  clicks      Int
  timeSeconds Float
  won         Boolean
  playedAt    DateTime    @default(now())

  @@unique([puzzleId, userId])
  @@index([puzzleId])
}
