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

datasource db {
  provider = "sqlite"
}

model InstalledScript {
  id                Int       @id @default(autoincrement())
  script_name       String
  script_path       String
  container_id      String?
  server_id         Int?
  execution_mode    String
  installation_date DateTime? @default(now())
  status            String
  output_log        String?
  web_ui_ip         String?
  web_ui_port       Int?
  server            Server?   @relation(fields: [server_id], references: [id], onDelete: SetNull)
  lxc_config        LXCConfig?
  
  @@map("installed_scripts")
}

model Server {
  id                 Int                @id @default(autoincrement())
  name               String             @unique
  ip                 String
  user               String
  password           String?
  auth_type          String?            @default("password")
  ssh_key            String?
  ssh_key_passphrase String?
  ssh_port           Int?               @default(22)
  color              String?
  created_at         DateTime?          @default(now())
  updated_at         DateTime?          @updatedAt
  ssh_key_path       String?
  key_generated      Boolean?           @default(false)
  installed_scripts  InstalledScript[]
  backups            Backup[]
  pbs_credentials    PBSStorageCredential[]
  
  @@map("servers")
}

model LXCConfig {
  id                    Int              @id @default(autoincrement())
  installed_script_id   Int              @unique
  installed_script      InstalledScript  @relation(fields: [installed_script_id], references: [id], onDelete: Cascade)
  
  // Basic settings
  arch                  String?
  cores                 Int?
  memory                Int?
  hostname              String?
  swap                  Int?
  onboot                Int?            // 0 or 1
  ostype                String?
  unprivileged          Int?            // 0 or 1
  
  // Network settings (net0)
  net_name              String?
  net_bridge            String?
  net_hwaddr            String?
  net_ip_type           String?         // 'dhcp' or 'static'
  net_ip                String?         // IP with CIDR for static
  net_gateway           String?
  net_type              String?         // usually 'veth'
  net_vlan              Int?
  
  // Storage
  rootfs_storage        String?
  rootfs_size           String?
  
  // Features
  feature_keyctl        Int?            // 0 or 1
  feature_nesting       Int?            // 0 or 1
  feature_fuse          Int?            // 0 or 1
  feature_mount         String?         // other mount features
  
  // Tags
  tags                  String?
  
  // Advanced/raw settings (lxc.* entries and other uncommon settings)
  advanced_config       String?         // Text blob for advanced settings
  
  // Metadata
  synced_at             DateTime?
  config_hash           String?         // Hash of server config for diff detection
  
  created_at            DateTime         @default(now())
  updated_at            DateTime         @updatedAt
  
  @@map("lxc_configs")
}

model Backup {
  id                Int       @id @default(autoincrement())
  container_id      String
  server_id         Int
  hostname          String
  backup_name       String
  backup_path       String
  size              BigInt?
  created_at        DateTime?
  storage_name      String
  storage_type      String    // 'local', 'storage', or 'pbs'
  discovered_at     DateTime  @default(now())
  server            Server    @relation(fields: [server_id], references: [id], onDelete: Cascade)
  
  @@index([container_id])
  @@index([server_id])
  @@map("backups")
}

model PBSStorageCredential {
  id                Int       @id @default(autoincrement())
  server_id         Int
  storage_name      String
  pbs_ip            String
  pbs_datastore     String
  pbs_username      String    @default("root@pam")
  pbs_password      String
  pbs_fingerprint   String
  created_at        DateTime  @default(now())
  updated_at        DateTime  @updatedAt
  server            Server    @relation(fields: [server_id], references: [id], onDelete: Cascade)
  
  @@unique([server_id, storage_name])
  @@index([server_id])
  @@map("pbs_storage_credentials")
}

model Repository {
  id          Int      @id @default(autoincrement())
  url         String   @unique
  enabled     Boolean  @default(true)
  is_default  Boolean  @default(false)
  is_removable Boolean @default(true)
  priority    Int      @default(0)
  created_at  DateTime @default(now())
  updated_at  DateTime @updatedAt
  
  @@map("repositories")
}

model ScriptNote {
  id          Int      @id @default(autoincrement())
  script_slug String
  title       String   @default("")
  content     String
  is_shared   Boolean  @default(false)
  created_at  DateTime @default(now())
  updated_at  DateTime @updatedAt

  @@index([script_slug])
  @@map("script_notes")
}

model ServerPreset {
  id              Int      @id @default(autoincrement())
  server_id       Int
  name            String
  cpu             Int?
  ram             Int?
  disk            Int?
  privileged      Boolean  @default(false)
  bridge          String?
  vlan            String?
  dns             String?
  ssh             Boolean  @default(false)
  nesting         Boolean  @default(true)
  fuse            Boolean  @default(false)
  apt_proxy_addr  String?
  apt_proxy_on    Boolean  @default(false)
  created_at      DateTime @default(now())
  updated_at      DateTime @updatedAt

  @@index([server_id])
  @@map("server_presets")
}
