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

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

model Player {
  id           String   @id @default(auto()) @map("_id") @db.ObjectId
  uuid         String   @unique @default(uuid())
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt
  anonymous    Boolean  @default(true)
  username     String   @unique @default("")
  email        String   @unique @default("")
  passwordHash String   @unique @default("")
  games        Game[]   @relation(fields: [gameIds], references: [id])
  gameIds      String[] @db.ObjectId
  tokens       Token[]
}

enum TokenType {
  REFRESH
  ACCESS
}

model Token {
  id        String    @id @default(auto()) @map("_id") @db.ObjectId
  createdAt DateTime  @default(now())
  expires   DateTime  @updatedAt
  owner     Player    @relation(fields: [ownerId], references: [id])
  ownerId   String    @db.ObjectId
  token     String    @unique
  type      TokenType
  used      Boolean   @default(false)
}

model Game {
  id        String    @id @default(auto()) @map("_id") @db.ObjectId
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  players   Player[]  @relation(fields: [playerIds], references: [id])
  playerIds String[]  @db.ObjectId
  running   Boolean   @default(true)
  Move      Move[]
  Gamepin   Gamepin[]
  Chat      Chat[]
}

model Move {
  id        String   @id @default(auto()) @map("_id") @db.ObjectId
  createdAt DateTime @default(now())
  // updatedAt DateTime @updatedAt
  index     Int
  game      Game     @relation(fields: [gameId], references: [id])
  gameId    String   @db.ObjectId
}

model Gamepin {
  id        String   @id @default(auto()) @map("_id") @db.ObjectId
  createdAt DateTime @default(now())
  // updatedAt DateTime @updatedAt
  pin       Int      @unique
  game      Game     @relation(fields: [gameId], references: [id])
  gameId    String   @db.ObjectId
}

model Chat {
  id        String   @id @default(auto()) @map("_id") @db.ObjectId
  createdAt DateTime @default(now())
  // updatedAt DateTime @updatedAt
  message   String   @default("")
  event     String   @default("")
  game      Game     @relation(fields: [gameId], references: [id])
  gameId    String   @db.ObjectId
}
