datasource db {
  provider = "sqlite"
}

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

enum Permission {
  supervisor_admin
  manage_agents
  remote_execution
  manage_hosts
  agent_communication
  manage_models
  manage_variables
  view_run_logs
}

model User {
  id                  Int                 @id @default(autoincrement())
  username            String              @unique
  uuid                String              @unique
  isAgent             Boolean             @default(false) @map("is_agent")
  passwordHash        String?             @map("password_hash")
  createdAt           DateTime            @default(now()) @map("created_at")
  apiKeyHash          String?             @unique @map("api_key_hash")
  updatedAt           DateTime            @updatedAt @map("updated_at")
  permissions         UserPermission[]    @relation("UserPermissions")
  grantedPermissions  UserPermission[]    @relation("GrantedByPermissions")
  sessions            Session[]
  passkeyCredentials  PasskeyCredential[]
  registrationTokens  RegistrationToken[]

  @@map("users")
}

model Session {
  id        Int      @id @default(autoincrement())
  userId    Int      @map("user_id")
  tokenHash String   @unique @map("token_hash")
  expiresAt DateTime @map("expires_at")
  createdAt DateTime @default(now()) @map("created_at")
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@map("sessions")
}

model UserPermission {
  id         Int        @id @default(autoincrement())
  userId     Int        @map("user_id")
  permission Permission
  grantedAt  DateTime   @default(now()) @map("granted_at")
  grantedBy  Int?       @map("granted_by")
  user       User       @relation("UserPermissions", fields: [userId], references: [id], onDelete: Cascade)
  grantedByUser User?   @relation("GrantedByPermissions", fields: [grantedBy], references: [id])

  @@unique([userId, permission])
  @@map("user_permissions")
}

model PasskeyCredential {
  id           Int       @id @default(autoincrement())
  userId       Int       @map("user_id")
  credentialId String    @unique @map("credential_id")
  publicKey    String    @map("public_key")
  counter      Int       @default(0)
  transports   String    @default("")
  deviceLabel  String    @default("") @map("device_label")
  createdAt    DateTime  @default(now()) @map("created_at")
  lastUsedAt   DateTime? @map("last_used_at")
  user         User      @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@map("passkey_credentials")
}

model RegistrationToken {
  id        Int       @id @default(autoincrement())
  userId    Int       @map("user_id")
  tokenHash String    @unique @map("token_hash")
  expiresAt DateTime  @map("expires_at")
  usedAt    DateTime? @map("used_at")
  createdAt DateTime  @default(now()) @map("created_at")
  user      User      @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@map("registration_tokens")
}

model SchemaVersion {
  id      Int    @id @default(1)
  version Int
  updated String

  @@map("schema_version")
}
