// Prisma Schema - 图片OCR与智能文档管理系统

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "linux-musl-openssl-3.0.x"]
}

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

// 用户
model User {
  id            String    @id @default(uuid())
  username      String    @unique
  email         String?   @unique
  password_hash String
  created_at    DateTime  @default(now())
  updated_at    DateTime  @updatedAt

  documents     Document[]
  todos         Todo[]
  categories    Category[]
  tags          Tag[]
  images        Image[]
  configs       Config[]

  @@map("users")
}

// 文档
model Document {
  id          String   @id @default(uuid())
  user_id     String
  title       String?
  content     String
  category_id String?
  created_at  DateTime @default(now())
  updated_at  DateTime @updatedAt

  user        User           @relation(fields: [user_id], references: [id], onDelete: Cascade)
  category    Category?      @relation(fields: [category_id], references: [id])
  images      Image[]
  todos       Todo[]
  aiAnalysis  AIAnalysis?

  @@index([user_id])
  @@index([category_id])
  @@map("documents")
}

// 图片
model Image {
  id                String   @id @default(uuid())
  user_id           String
  document_id       String?
  file_path         String
  file_size         Int
  mime_type         String
  ocr_result        String?
  ocr_confidence    Float?
  processing_status String   @default("pending")
  quality_score     Float?
  error_message     String?
  created_at        DateTime @default(now())
  updated_at        DateTime @updatedAt

  user              User      @relation(fields: [user_id], references: [id], onDelete: Cascade)
  document          Document? @relation(fields: [document_id], references: [id], onDelete: SetNull)

  @@index([user_id])
  @@index([processing_status])
  @@index([document_id])
  @@map("images")
}

// 待办事项
model Todo {
  id           String    @id @default(uuid())
  user_id      String
  document_id  String?
  title        String
  description  String?
  priority     String    @default("medium")
  status       String    @default("pending")
  due_date     DateTime?
  category_id  String?
  completed_at DateTime?
  confirmed_at DateTime?
  created_at   DateTime  @default(now())
  updated_at   DateTime  @updatedAt

  user         User      @relation(fields: [user_id], references: [id], onDelete: Cascade)
  document     Document? @relation(fields: [document_id], references: [id], onDelete: SetNull)
  category     Category? @relation(fields: [category_id], references: [id])

  @@index([user_id])
  @@index([status])
  @@index([category_id])
  @@map("todos")
}

// 分类
model Category {
  id            String   @id @default(uuid())
  user_id       String
  name          String
  type          String
  color         String?
  icon          String?
  parent_id     String?
  sort_order    Int      @default(0)
  usage_count   Int      @default(0)
  is_ai_created Boolean  @default(false)
  created_at    DateTime @default(now())

  user          User       @relation(fields: [user_id], references: [id], onDelete: Cascade)
  parent        Category?  @relation("CategoryToCategory", fields: [parent_id], references: [id])
  children      Category[] @relation("CategoryToCategory")
  documents     Document[]
  todos         Todo[]

  @@index([user_id])
  @@index([type])
  @@map("categories")
}

// 标签
model Tag {
  id            String   @id @default(uuid())
  user_id       String
  name          String
  color         String?
  usage_count   Int      @default(0)
  is_ai_created Boolean  @default(false)
  created_at    DateTime @default(now())

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

  @@unique([user_id, name])
  @@index([user_id])
  @@map("tags")
}

// AI分析结果
model AIAnalysis {
  id                String   @id @default(uuid())
  document_id       String   @unique
  provider          String
  model             String
  suggested_tags    String
  suggested_category String?
  summary           String?
  raw_response      String
  created_at        DateTime @default(now())

  document Document @relation(fields: [document_id], references: [id], onDelete: Cascade)

  @@map("ai_analyses")
}

// 配置
model Config {
  id        String   @id @default(uuid())
  user_id   String
  key       String
  value     String
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt

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

  @@unique([user_id, key])
  @@index([user_id])
  @@map("configs")
}
