generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model Player {
  id           String        @id @default(cuid())
  createdAt    DateTime      @default(now())
  updatedAt    DateTime      @updatedAt
  anonymous    Boolean       @default(true)
  username     String?       @unique
  email        String?       @unique
  passwordHash String?       @unique
  tokens       Token[]
  games        Player_Game[]
}

enum TokenType {
  REFRESH
  ACCESS
}

model Token {
  id        String    @id @default(cuid())
  createdAt DateTime  @default(now())
  token     String    @unique
  type      TokenType
  used      Boolean   @default(false)
  ownerId   String
  owner     Player    @relation(fields: [ownerId], references: [id])
  Socket    Socket?
}

model Game {
  id        String        @id @default(cuid())
  createdAt DateTime      @default(now())
  updatedAt DateTime      @updatedAt
  running   Boolean       @default(true)
  pin       Gamepin?
  players   Player_Game[]
}

model Player_Game {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now())
  gameId    String
  playerId  String
  isOwner   Boolean
  moves     Move[]
  chats     Chat[]
  game      Game     @relation(fields: [gameId], references: [id])
  player    Player   @relation(fields: [playerId], references: [id])

  @@unique([gameId, playerId])
}

model Move {
  id        String      @id @default(cuid())
  createdAt DateTime    @default(now())
  index     Int
  gameId    String
  game      Player_Game @relation(fields: [gameId], references: [id])
}

model Gamepin {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now())
  pin       Int      @unique
  gameId    String   @unique
  game      Game     @relation(fields: [gameId], references: [id])
}

model Chat {
  id        String      @id @default(cuid())
  createdAt DateTime    @default(now())
  message   String?
  event     String?
  gameId    String
  game      Player_Game @relation(fields: [gameId], references: [id])
}

model Socket {
  sessionId String   @id
  createdAt DateTime @default(now())
  tokenId   String   @unique
  token     Token    @relation(fields: [tokenId], references: [id])
}
