// ============================================
// AI Chat Module - Database Schema Template
// ============================================
//
// Copy these models to your main schema.prisma file.
// Make sure your User model exists and has an id field.
//
// Required tables:
// - Conversation (会话管理)
// - Interaction (交互记录 - Event Sourcing)
// - Agent (AI 代理配置)
//
// All tables use cascade delete to ensure data consistency.
// ============================================

// Conversation - 会话管理
model Conversation {
  id           String        @id @default(cuid())
  userId       String        @map("user_id")
  title        String?
  // Extensible metadata (e.g., projectId, teamId, etc.)
  metadata     Json?
  createdAt    DateTime      @default(now()) @map("created_at")
  updatedAt    DateTime      @updatedAt @map("updated_at")
  
  user         User          @relation(fields: [userId], references: [id], onDelete: Cascade)
  interactions Interaction[]

  @@index([updatedAt])
  @@index([userId, createdAt])
  @@map("conversations")
}

// Interaction - 交互记录 (Event Sourcing pattern)
model Interaction {
  id             String   @id @default(cuid())
  conversationId String   @map("conversation_id")
  // Interaction type: 'user_message' | 'ai_message' | 'document_processing' | custom
  type           String
  // Vercel AI SDK: UIMessage parts (all data stored here as JSON)
  parts          Json     @default("[]")
  // Additional metadata (agentId, model, usage, etc.)
  metadata       Json?
  // Reference to related interaction (e.g., document_processing -> user_message)
  correlationId  String?  @map("correlation_id")
  createdAt      DateTime @default(now()) @map("created_at")

  conversation Conversation @relation(fields: [conversationId], references: [id], onDelete: Cascade)

  @@index([conversationId, createdAt])
  @@index([correlationId])
  @@map("interactions")
}

// Agent - AI 代理配置
model Agent {
  id           String   @id @default(cuid())
  userId       String   @map("user_id")

  name         String
  description  String?
  instructions String   @db.Text
  model        String   @default("google/gemini-2.5-pro")

  // Avatar/Icon
  avatar       String?
  color        String?

  // Tool names from registry (e.g., ["document_tools", "image_tools"])
  tools        String[] @default([])

  enabled      Boolean  @default(true)
  isDefault    Boolean  @default(false) @map("is_default")
  isSystem     Boolean  @default(false) @map("is_system") // System preset agent, visible to all users

  createdAt    DateTime @default(now()) @map("created_at")
  updatedAt    DateTime @updatedAt @map("updated_at")

  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@index([userId])
  @@index([userId, isDefault])
  @@index([isSystem, enabled])
  @@map("agents")
}

// ============================================
// User Model Relation (Add to your existing User model)
// ============================================
//
// model User {
//   // ... your existing fields
//   conversations Conversation[]
//   agents        Agent[]
// }
//
// ============================================

