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

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

enum UserRole {
  admin
  user
}

enum SignalStatus {
  pending
  win
  lose
  void
  manual_review
  unpublished
}

enum SourceType {
  manual
  provider
}

enum SubscriptionStatus {
  active
  expired
  canceled
}

model User {
  id                   String                 @id @default(cuid())
  email                String                 @unique
  passwordHash         String
  role                 UserRole               @default(user)
  active               Boolean                @default(true)
  sessionVersion       Int                    @default(0)
  createdAt            DateTime               @default(now())
  updatedAt            DateTime               @updatedAt
  notificationSetting  NotificationSetting?
  pushSubscriptions    PushSubscription[]
  nativePushSubscriptions NativePushSubscription[]
  botAccesses          UserBotAccess[]
  passwordResetTokens  PasswordResetToken[]
  adminActions         AdminActionLog[]
}

model PasswordResetToken {
  id         String   @id @default(cuid())
  userId     String
  tokenHash  String   @unique
  expiresAt  DateTime
  usedAt     DateTime?
  createdAt  DateTime @default(now())
  user       User     @relation(fields: [userId], references: [id], onDelete: Cascade)

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

model Bot {
  id          String          @id @default(cuid())
  key         String          @unique
  name        String
  sourceUrl   String
  active      Boolean         @default(true)
  createdAt   DateTime        @default(now())
  updatedAt   DateTime        @updatedAt
  userAccesses UserBotAccess[]
}

model UserBotAccess {
  id          String   @id @default(cuid())
  userId      String
  botId       String
  status      SubscriptionStatus @default(active)
  startsAt    DateTime @default(now())
  expiresAt   DateTime?
  notes       String?
  grantedAt   DateTime @default(now())
  grantedById String?
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
  user        User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  bot         Bot      @relation(fields: [botId], references: [id], onDelete: Cascade)

  @@unique([userId, botId])
}

model Signal {
  id               String               @id @default(cuid())
  providerId       String?
  eventId          String
  sportType        String
  leagueName       String
  homeTeam         String
  awayTeam         String
  eventStartTime   DateTime
  marketType       String
  selection        String
  forecast         String?
  lineValue        Float?
  odds             Float
  signalTime       DateTime
  status           SignalStatus         @default(pending)
  sourceType       SourceType
  comment          String?
  published        Boolean              @default(true)
  dedupeKey        String               @unique
  rawPayload       Json?
  createdAt        DateTime             @default(now())
  updatedAt        DateTime             @updatedAt
  settlement       Settlement?
  notifications    NotificationLog[]
}

model EventResult {
  id          String   @id @default(cuid())
  eventId     String   @unique
  homeScore   Int?
  awayScore   Int?
  status      String
  payload     Json?
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
}

model Settlement {
  id           String       @id @default(cuid())
  signalId     String       @unique
  result       SignalStatus
  explanation  String
  createdAt    DateTime     @default(now())
  updatedAt    DateTime     @updatedAt
  signal       Signal       @relation(fields: [signalId], references: [id], onDelete: Cascade)
}

model PushSubscription {
  id          String   @id @default(cuid())
  userId      String
  endpoint    String
  p256dh      String
  auth        String
  active      Boolean  @default(true)
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
  user        User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([userId, endpoint])
}

model NativePushSubscription {
  id          String   @id @default(cuid())
  userId      String
  token       String
  platform    String
  deviceId    String?
  active      Boolean  @default(true)
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
  user        User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([userId, token])
}

model NotificationSetting {
  id                  String   @id @default(cuid())
  userId              String   @unique
  signalsPushEnabled  Boolean  @default(true)
  resultsPushEnabled  Boolean  @default(false)
  createdAt           DateTime @default(now())
  updatedAt           DateTime @updatedAt
  user                User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model NotificationLog {
  id           String   @id @default(cuid())
  signalId      String?
  type          String
  recipients    Int
  successCount  Int
  failedCount   Int
  payload       Json?
  createdAt     DateTime @default(now())
  signal        Signal?   @relation(fields: [signalId], references: [id], onDelete: SetNull)
}

model AdminActionLog {
  id          String   @id @default(cuid())
  adminId     String
  action      String
  entityType  String
  entityId    String
  metadata    Json?
  createdAt   DateTime @default(now())
  admin       User     @relation(fields: [adminId], references: [id], onDelete: Cascade)
}

model IntegrationLog {
  id          String   @id @default(cuid())
  provider    String
  level       String
  message     String
  payload     Json?
  createdAt   DateTime @default(now())
}
