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

datasource db {
  provider = "postgresql"
}

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

enum RoutingMode {
  AUTO
  MANUAL_MODEL
  LOCAL_ONLY
  PRIVACY_FIRST
  LOW_LATENCY
  HIGH_REASONING
  COST_SAVER
}

enum MessageRole {
  SYSTEM
  USER
  ASSISTANT
  TOOL
}

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

model ChatThread {
  id           String      @id @default(cuid())
  userId       String      @map("user_id")
  title        String?
  routingMode  RoutingMode @default(AUTO) @map("routing_mode")
  lastProvider String?     @map("last_provider")
  lastModel    String?     @map("last_model")
  isPinned     Boolean     @default(false) @map("is_pinned")
  isArchived   Boolean     @default(false) @map("is_archived")
  preferredProvider String?  @map("preferred_provider")
  preferredModel    String?  @map("preferred_model")
  contextPackIds    String[] @default([]) @map("context_pack_ids")
  systemPrompt      String?  @map("system_prompt")
  temperature       Float?   @default(0.7) @map("temperature")
  maxTokens         Int?     @map("max_tokens")
  judgeEnabled      Boolean  @default(false) @map("judge_enabled")
  judgeModel        String?  @map("judge_model")
  qualityThreshold  Float?   @map("quality_threshold")
  maxReRouteAttempts Int?    @map("max_reroute_attempts")
  // === Integration V2 — per-thread memory/context switches ===
  useMemory    Boolean     @default(true) @map("use_memory")
  useContext   Boolean     @default(true) @map("use_context")
  createdAt    DateTime    @default(now()) @map("created_at")
  updatedAt    DateTime    @updatedAt @map("updated_at")

  messages ChatMessage[]

  @@index([userId])
  @@index([createdAt])
  @@map("chat_threads")
}

model ChatMessageContextReceipt {
  id          String   @id @default(cuid())
  messageId   String   @unique @map("message_id")
  threadId    String   @map("thread_id")
  userId      String   @map("user_id")
  payloadJson Json     @map("payload_json")
  createdAt   DateTime @default(now()) @map("created_at")

  @@index([threadId])
  @@index([userId, createdAt])
  @@map("chat_message_context_receipts")
}

model ChatMessage {
  id            String       @id @default(cuid())
  threadId      String       @map("thread_id")
  role          MessageRole
  content       String
  provider      String?
  model         String?
  routingMode   RoutingMode? @map("routing_mode")
  routerModel   String?      @map("router_model")
  usedFallback  Boolean      @default(false) @map("used_fallback")
  inputTokens   Int?         @map("input_tokens")
  outputTokens  Int?         @map("output_tokens")
  estimatedCost Decimal?     @map("estimated_cost") @db.Decimal(12, 8)
  latencyMs     Int?         @map("latency_ms")
  feedback      String?      @map("feedback")
  metadata      Json?
  createdAt     DateTime     @default(now()) @map("created_at")

  thread               ChatThread           @relation(fields: [threadId], references: [id], onDelete: Cascade)
  attachments          MessageAttachment[]
  fileDeliveryRecords  FileDeliveryRecord[]

  @@index([threadId])
  @@index([createdAt])
  @@map("chat_messages")
}

model MessageAttachment {
  id        String @id @default(cuid())
  messageId String @map("message_id")
  fileId    String @map("file_id")
  type      String

  message ChatMessage @relation(fields: [messageId], references: [id], onDelete: Cascade)

  @@index([messageId])
  @@map("message_attachments")
}

model FileDeliveryRecord {
  id             String   @id @default(uuid())
  messageId      String   @map("message_id")
  threadId       String   @map("thread_id")
  userId         String   @map("user_id")
  fileId         String   @map("file_id")
  filename       String
  mimeType       String   @map("mime_type")
  provider       String
  model          String
  mode           String
  supportsVision Boolean  @map("supports_vision")
  createdAt      DateTime @default(now()) @map("created_at")

  message ChatMessage @relation(fields: [messageId], references: [id], onDelete: Cascade)

  @@index([messageId])
  @@index([fileId, provider, model])
  @@index([threadId, fileId])
  @@map("file_delivery_records")
}
