// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
  provider = "prisma-client-js"
}

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

model User {
  id                String   @id // 使用Supabase用户ID，不再自动生成
  email             String   @unique
  username          String?  @unique // 允许为空，因为有些用户可能没有设置用户名
  avatar            String?
  bio               String?
  language          String   @default("en")
  isAdmin           Boolean  @default(false) // 管理员标记
  versionLimit      Int      @default(3) // 版本数量限制，可在用户配置中设置
  subscribePlan     String   @default("free") // 订阅计划: "free", "pro"
  maxVersionLimit   Int      @default(3) // 基于订阅的最大版本限制
  promptLimit       Int      @default(20) // 提示词数量限制
  creditBalance     Float    @default(5.0) // 信用余额，单位：美元
  createdAt         DateTime @default(now())
  updatedAt         DateTime @updatedAt

  prompts Prompt[]
  credits Credit[]

  @@map("users")
}

model Prompt {
  id          String   @id @default(cuid())
  name        String
  content     String
  description String?
  isPublic    Boolean  @default(false) // 保留用于向后兼容
  permissions String   @default("private") // "private" | "public"
  visibility  String?  // "under_review" | "published" | null
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt

  userId   String
  user     User               @relation(fields: [userId], references: [id], onDelete: Cascade)
  tags     PromptTag[]
  versions PromptVersion[]
  album    PromptAlbum?       @relation(fields: [albumId], references: [id])
  albumId  String?
  tests    PromptTestRun[]
  stats    PromptStats?

  @@map("prompts")
}

model PromptVersion {
  id        String   @id @default(cuid())
  version   Int
  content   String
  changelog String?
  createdAt DateTime @default(now())

  promptId String
  prompt   Prompt @relation(fields: [promptId], references: [id], onDelete: Cascade)

  @@unique([promptId, version])
  @@map("prompt_versions")
}

model PromptTag {
  id    String @id @default(cuid())
  name  String @unique
  color String @default("#3B82F6")

  prompts Prompt[]

  @@map("prompt_tags")
}

model PromptAlbum {
  id          String   @id @default(cuid())
  name        String
  description String?
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt

  prompts Prompt[]

  @@map("prompt_albums")
}

model PromptTestRun {
  id        String   @id @default(cuid())
  input     String
  output    String?
  success   Boolean  @default(false)
  error     String?
  createdAt DateTime @default(now())

  promptId String
  prompt   Prompt @relation(fields: [promptId], references: [id], onDelete: Cascade)

  @@map("prompt_test_runs")
}

model PromptStats {
  id          String   @id @default(cuid())
  promptId    String   @unique
  viewCount   Int      @default(0) // 浏览计数
  likeCount   Int      @default(0) // 点赞计数
  rating      Float?   // 平均评分
  ratingCount Int      @default(0) // 评分数量
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt

  prompt Prompt @relation(fields: [promptId], references: [id], onDelete: Cascade)

  @@map("prompt_stats")
}

model Credit {
  id          String    @id @default(cuid())
  userId      String
  amount      Float     // 信用额度数量
  type        String    // "system_gift", "subscription_monthly", "user_purchase"
  note        String?   // 备注说明
  expiresAt   DateTime? // 过期时间，null表示永久有效
  isActive    Boolean   @default(true) // 是否激活状态
  createdAt   DateTime  @default(now())
  updatedAt   DateTime  @updatedAt

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

  @@map("credits")
}
