generator client {
  provider = "prisma-client-js"
  output   = "../../../node_modules/.prisma/client"
}

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

model User {
  id           String     @id @default(uuid()) @db.Uuid
  opaqueUserId String     @unique @map("twitch_opaque_user_id")
  createdAt    DateTime   @default(now()) @map("created_at")
  survivors    Survivor[]

  @@map("users")
}

model Survivor {
  id           String               @id @default(uuid()) @db.Uuid
  userId       String               @map("user_id") @db.Uuid
  user         User                 @relation(fields: [userId], references: [id])
  channelId    String               @map("channel_id")
  name         String               @db.VarChar(32)
  state        String               @default("active")
  stats        Json
  perkSlots    Json                 @map("perk_slots")
  createdAt    DateTime             @default(now()) @map("created_at")
  participants MissionParticipant[]

  @@map("survivors")
}

model Mission {
  id                      String               @id @default(uuid()) @db.Uuid
  groupId                 String?              @map("group_id") @db.Uuid
  channelId               String               @map("channel_id")
  difficulty              Int                  @db.SmallInt
  durationMinutes         Int                  @default(20) @map("duration_minutes") @db.SmallInt
  status                  String               @default("active")
  encounterLibraryVersion String               @map("encounter_library_version")
  startedAt               DateTime             @default(now()) @map("started_at")
  endedAt                 DateTime?            @map("ended_at")
  tickIndex               Int                  @default(0) @map("tick_index")
  nextTickAt              DateTime             @map("next_tick_at")
  participants            MissionParticipant[]
  logs                    MissionLog[]

  @@index([channelId])
  @@index([status, nextTickAt])
  @@map("missions")
}

model MissionParticipant {
  id         String   @id @default(uuid()) @db.Uuid
  missionId  String   @map("mission_id") @db.Uuid
  mission    Mission  @relation(fields: [missionId], references: [id])
  survivorId String   @map("survivor_id") @db.Uuid
  survivor   Survivor @relation(fields: [survivorId], references: [id])
  state      String   @default("active")
  hookCount  Int      @default(0) @map("hook_count") @db.SmallInt

  @@unique([missionId, survivorId])
  @@map("mission_participants")
}

model MissionLog {
  id               String   @id @default(uuid()) @db.Uuid
  missionId        String   @map("mission_id") @db.Uuid
  mission          Mission  @relation(fields: [missionId], references: [id])
  tickIndex        Int      @map("tick_index")
  encounterKey     String   @map("encounter_key")
  renderedText     String   @map("rendered_text")
  seed             String
  modifiersApplied Json     @map("modifiers_applied")
  createdAt        DateTime @default(now()) @map("created_at")

  @@index([missionId, tickIndex])
  @@map("mission_logs")
}
