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

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

model Room {
  id        String   @id
  data      String
  createdAt DateTime @default(now())
  expiresAt DateTime

  @@index([expiresAt])
}

model User {
  id           String     @id @default(cuid())
  username     String     @unique
  passwordHash String
  avatar       String     @default("🐱")
  email        String?    @unique
  preferences  String     @default("{}")
  createdAt    DateTime   @default(now())
  decisions    Decision[]
  favorites    Favorite[]

  createdBlindBoxRooms BlindBoxRoom[]   @relation("RoomCreator")
  blindBoxMemberships  BlindBoxMember[]
  submittedIdeas       BlindBoxIdea[]   @relation("IdeaSubmitter")
  drawnIdeas           BlindBoxIdea[]   @relation("IdeaDrawer")
  weekendPlans         WeekendPlan[]
}

model Decision {
  id             String   @id @default(cuid())
  userId         String
  roomId         String
  restaurantName String
  restaurantData String
  matchType      String
  participants   Int
  createdAt      DateTime @default(now())
  user           User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@index([userId])
  @@index([roomId])
}

model Favorite {
  id             String   @id @default(cuid())
  userId         String
  restaurantId   String   @default("")
  restaurantData String
  createdAt      DateTime @default(now())
  user           User     @relation(fields: [userId], references: [id], onDelete: Cascade)

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

model BlindBoxRoom {
  id        String   @id @default(cuid())
  code      String   @unique
  name      String
  creatorId String
  city      String?
  lat       Float?
  lng       Float?
  createdAt DateTime @default(now())

  creator User             @relation("RoomCreator", fields: [creatorId], references: [id])
  members BlindBoxMember[]
  ideas   BlindBoxIdea[]
  plans   WeekendPlan[]
}

model BlindBoxMember {
  id       String   @id @default(cuid())
  roomId   String
  userId   String
  joinedAt DateTime @default(now())

  room BlindBoxRoom @relation(fields: [roomId], references: [id], onDelete: Cascade)
  user User         @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([roomId, userId])
}

model BlindBoxIdea {
  id        String   @id @default(uuid())
  roomId    String
  userId    String
  content   String
  status    String   @default("in_pool")
  drawnById String?
  createdAt DateTime @default(now())

  category         String?
  timeSlot         String?
  estimatedMinutes Int?
  outdoor          Boolean?
  searchQuery      String?
  searchType       String?

  room    BlindBoxRoom @relation(fields: [roomId], references: [id], onDelete: Cascade)
  user    User         @relation("IdeaSubmitter", fields: [userId], references: [id], onDelete: Cascade)
  drawnBy User?        @relation("IdeaDrawer", fields: [drawnById], references: [id], onDelete: SetNull)

  @@index([roomId, status])
  @@index([userId])
}

model WeekendPlan {
  id        String   @id @default(cuid())
  roomId    String
  userId    String
  planData  String
  status    String   @default("active")
  createdAt DateTime @default(now())

  room BlindBoxRoom @relation(fields: [roomId], references: [id], onDelete: Cascade)
  user User         @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@index([roomId])
}
