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

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

model User {
  id             Int              @id @default(autoincrement())
  uuid           String           @unique @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  username       String
  password       String?
  avatar         String?
  token          String
  administrator  Boolean          @default(false)
  superAdmin     Boolean          @default(false)
  systemTheme    String           @default("system")
  embed          Json             @default("{}")
  ratelimit      DateTime?
  totpSecret     String?
  domains        String[]
  oauth          OAuth[]
  files          File[]
  urls           Url[]
  Invite         Invite[]
  Folder         Folder[]
  IncompleteFile IncompleteFile[]
  Exports        Export[]
}

model Export {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  complete  Boolean  @default(false)

  path String

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

model Folder {
  id        Int      @id @default(autoincrement())
  name      String
  public    Boolean  @default(false)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

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

  files File[]
}

model File {
  id           Int            @id @default(autoincrement())
  name         String
  originalName String?
  mimetype     String         @default("image/png")
  createdAt    DateTime       @default(now())
  size         BigInt         @default(0)
  expiresAt    DateTime?
  maxViews     Int?
  views        Int            @default(0)
  favorite     Boolean        @default(false)
  embed        Boolean        @default(false)
  password     String?
  invisible    InvisibleFile?

  user   User? @relation(fields: [userId], references: [id], onDelete: SetNull)
  userId Int?

  folder   Folder? @relation(fields: [folderId], references: [id], onDelete: SetNull)
  folderId Int?

  thumbnail Thumbnail?
}

model Thumbnail {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  name      String

  fileId Int  @unique
  file   File @relation(fields: [fileId], references: [id], onDelete: Cascade)
}

model InvisibleFile {
  id    Int    @id @default(autoincrement())
  invis String @unique

  fileId Int  @unique
  file   File @relation(fields: [fileId], references: [id], onDelete: Cascade)
}

model Url {
  id          String        @id @unique
  destination String
  vanity      String?
  createdAt   DateTime      @default(now())
  maxViews    Int?
  views       Int           @default(0)
  invisible   InvisibleUrl?

  user   User? @relation(fields: [userId], references: [id], onDelete: SetNull)
  userId Int?
}

model InvisibleUrl {
  id    Int    @id @default(autoincrement())
  invis String @unique

  urlId String @unique
  url   Url    @relation(fields: [urlId], references: [id], onDelete: Cascade)
}

model Stats {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  data      Json
}

model Invite {
  id        Int       @id @default(autoincrement())
  code      String    @unique
  createdAt DateTime  @default(now())
  expiresAt DateTime?
  used      Boolean   @default(false)

  createdBy   User @relation(fields: [createdById], references: [id], onDelete: Cascade)
  createdById Int
}

model OAuth {
  id       Int            @id @default(autoincrement())
  provider OauthProviders
  user     User           @relation(fields: [userId], references: [uuid], onDelete: Cascade)
  userId   String         @db.Uuid
  username String
  oauthId  String?
  token    String
  refresh  String?

  @@unique([provider, oauthId])
}

enum OauthProviders {
  DISCORD
  GITHUB
  GOOGLE
}

model IncompleteFile {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())

  status         ProcessingStatus
  chunks         Int
  chunksComplete Int

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

  data Json
}

enum ProcessingStatus {
  PENDING
  PROCESSING
  COMPLETE
}
