// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

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

model User {
  id            Int      @id @default(autoincrement())
  email         String   @unique
  password      String
  roles         Json     @default("[\"ROLE_USER\"]")
  authenticated Boolean  @default(true)
  createdAt     DateTime @default(now())
  updatedAt     DateTime @updatedAt

  // Relations
  config      KomgaConfig?
  ttlConfig   TTLConfig?
  preferences Preferences?
  favorites   Favorite[]

  @@map("users")
}

model KomgaConfig {
  id         Int      @id @default(autoincrement())
  userId     Int      @unique
  url        String
  username   String
  authHeader String
  createdAt  DateTime @default(now())
  updatedAt  DateTime @updatedAt

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

  @@map("komgaconfigs")
}

model TTLConfig {
  id               Int      @id @default(autoincrement())
  userId           Int      @unique
  defaultTTL       Int      @default(5)
  homeTTL          Int      @default(5)
  librariesTTL     Int      @default(1440)
  seriesTTL        Int      @default(5)
  booksTTL         Int      @default(5)
  imagesTTL        Int      @default(1440)
  imageCacheMaxAge Int      @default(2592000) // 30 jours en secondes
  createdAt        DateTime @default(now())
  updatedAt        DateTime @updatedAt

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

  @@map("ttlconfigs")
}

model Preferences {
  id                      Int      @id @default(autoincrement())
  userId                  Int      @unique
  showThumbnails          Boolean  @default(true)
  cacheMode               String   @default("memory") // "memory" | "file"
  showOnlyUnread          Boolean  @default(false)
  displayMode             Json
  background              Json
  komgaMaxConcurrentRequests Int   @default(5)
  circuitBreakerConfig    Json
  readerPrefetchCount     Int      @default(5)
  createdAt               DateTime @default(now())
  updatedAt               DateTime @updatedAt

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

  @@map("preferences")
}

model Favorite {
  id        Int      @id @default(autoincrement())
  userId    Int
  seriesId  String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

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

  @@unique([userId, seriesId])
  @@index([userId])
  @@map("favorites")
}

