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

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

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

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

model User {
  id        String    @id @default(cuid())
  username  String    @unique
  name      String
  email     String    @unique
  password  String
  lastLogin DateTime?
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt

  @@map("users")
}

model Site {
  id          Int       @id @default(autoincrement())
  name        String
  description String?
  networks    Network[]

  @@map("sites")
}

model Network {
  id         Int      @id @default(autoincrement())
  site       Site     @relation(fields: [siteId], references: [id], onDelete: Cascade)
  siteId     Int
  name       String
  ipv4Subnet String?
  ipv6Subnet String?
  gateway    String?
  servers    Server[]

  @@map("networks")
}

model Server {
  id             Int                @id @default(autoincrement())
  network        Network            @relation(fields: [networkId], references: [id], onDelete: Cascade)
  networkId      Int
  name           String
  description    String?
  icon           String?
  ipv4Address    String?
  osDetails      String?
  cpuDetails     String?
  gpuDetails     String?
  memoryDetails  String?
  storageDetails String?
  managementUrl  String?
  monitoring     Boolean            @default(false)
  monitoringUrl  String?
  monitoringData ServerMonitoring[]
  applications   Application[]

  @@map("servers")
}

model ServerMonitoring {
  id                 Int      @id @default(autoincrement())
  server             Server   @relation(fields: [serverId], references: [id], onDelete: Cascade)
  serverId           Int
  cpuPercentUsage    Float
  gpuPercentUsage    Float
  memoryUsage        Float
  memoryPercentUsage Float
  diskUsage          Float
  diskPercentUsage   Float
  temperature        Float
  online             Boolean
  uptimeSeconds      Int
  timestamp          DateTime @default(now())

  @@index([serverId, timestamp])
  @@index([online])
  @@map("server_monitoring")
}

model Application {
  id             Int                     @id @default(autoincrement())
  server         Server                  @relation(fields: [serverId], references: [id], onDelete: Cascade)
  serverId       Int
  name           String
  description    String?
  icon           String?
  url            String?
  monitoring     Boolean                 @default(false)
  monitoringData ApplicationMonitoring[]

  @@map("applications")
}

model ApplicationMonitoring {
  id            Int         @id @default(autoincrement())
  application   Application @relation(fields: [applicationId], references: [id], onDelete: Cascade)
  applicationId Int
  online        Boolean
  latency       Float
  timestamp     DateTime    @default(now())

  @@index([applicationId, timestamp])
  @@index([online])
  @@map("application_monitoring")
}

model ServerMonitoringNotification {
  id               Int     @id @default(autoincrement())
  statusChange     Boolean
  cpuLimit         Float
  gpuLimit         Float
  memoryLimit      Float
  diskLimit        Float
  temperatureLimit Float

  @@map("server_monitoring_notifications")
}

model ApplicationMonitoringNotification {
  id           Int     @id @default(autoincrement())
  statusChange Boolean
  latencyLimit Float

  @@map("application_monitoring_notifications")
}

model NotificationProvider {
  id     Int                @id @default(autoincrement())
  name   String
  type   NotificationType
  config Json
  tests  NotificationTest[]

  @@map("notification_providers")
}

model NotificationTest {
  id                     Int                  @id @default(autoincrement())
  notificationProvider   NotificationProvider @relation(fields: [notificationProviderId], references: [id], onDelete: Cascade)
  notificationProviderId Int
  success                Boolean

  @@map("notification_tests")
}

enum NotificationType {
  TELEGRAM
}
