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

datasource db {
  provider = "postgresql"
}

// ─── Enums ───────────────────────────────────────────────

enum ConnectorProvider {
  OPENAI
  ANTHROPIC
  GEMINI
  AWS_BEDROCK
  DEEPSEEK
  OLLAMA
  GROK
  LLAMACPP
}

enum ConnectorStatus {
  HEALTHY
  DEGRADED
  DOWN
  UNKNOWN
}

enum ConnectorAuthType {
  API_KEY
  OAUTH2
  NONE
}

enum ModelLifecycle {
  ACTIVE
  DEPRECATED
  SUNSET
}

enum ModelSyncStatus {
  RUNNING
  COMPLETED
  FAILED
}

// ─── Models ──────────────────────────────────────────────

model Connector {
  id              String            @id @default(cuid())
  name            String
  provider        ConnectorProvider
  status          ConnectorStatus   @default(UNKNOWN)
  authType        ConnectorAuthType @map("auth_type")
  encryptedConfig String?           @map("encrypted_config")
  isEnabled       Boolean           @default(true) @map("is_enabled")
  defaultModelId  String?           @map("default_model_id")
  baseUrl         String?           @map("base_url")
  region          String?
  createdAt       DateTime          @default(now()) @map("created_at")
  updatedAt       DateTime          @updatedAt @map("updated_at")

  models       ConnectorModel[]
  healthEvents ConnectorHealthEvent[]
  syncRuns     ModelSyncRun[]

  @@index([provider])
  @@index([status])
  @@index([isEnabled])
  @@map("connectors")
}

model ConnectorModel {
  id                       String            @id @default(cuid())
  connectorId              String            @map("connector_id")
  provider                 ConnectorProvider
  modelKey                 String            @map("model_key")
  displayName              String            @map("display_name")
  lifecycle                ModelLifecycle    @default(ACTIVE)
  supportsStreaming        Boolean           @default(false) @map("supports_streaming")
  supportsTools            Boolean           @default(false) @map("supports_tools")
  supportsVision           Boolean           @default(false) @map("supports_vision")
  supportsAudio            Boolean           @default(false) @map("supports_audio")
  supportsStructuredOutput Boolean           @default(false) @map("supports_structured_output")
  maxContextTokens         Int?              @map("max_context_tokens")
  syncedAt                 DateTime          @default(now()) @map("synced_at")

  connector Connector @relation(fields: [connectorId], references: [id], onDelete: Cascade)

  @@unique([connectorId, modelKey])
  @@index([connectorId])
  @@index([provider])
  @@index([lifecycle])
  @@map("connector_models")
}

model ConnectorHealthEvent {
  id           String          @id @default(cuid())
  connectorId  String          @map("connector_id")
  status       ConnectorStatus
  latencyMs    Int?            @map("latency_ms")
  errorMessage String?         @map("error_message")
  checkedAt    DateTime        @default(now()) @map("checked_at")

  connector Connector @relation(fields: [connectorId], references: [id], onDelete: Cascade)

  @@index([connectorId])
  @@index([checkedAt])
  @@map("connector_health_events")
}

model ModelSyncRun {
  id            String          @id @default(cuid())
  connectorId   String          @map("connector_id")
  status        ModelSyncStatus
  modelsFound   Int             @default(0) @map("models_found")
  modelsAdded   Int             @default(0) @map("models_added")
  modelsRemoved Int             @default(0) @map("models_removed")
  startedAt     DateTime        @default(now()) @map("started_at")
  completedAt   DateTime?       @map("completed_at")
  errorMessage  String?         @map("error_message")

  connector Connector @relation(fields: [connectorId], references: [id], onDelete: Cascade)

  @@index([connectorId])
  @@index([status])
  @@map("model_sync_runs")
}
