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

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

model Author {
  // 抖音作者；以 sec_uid 稳定标识
  sec_uid         String   @id
  uid             String?  @unique
  nickname        String
  signature       String?
  avatar_url      String?
  follower_count  BigInt   @default(0)
  total_favorited BigInt   @default(0)
  unique_id       String?  // 抖音号
  short_id        String?
  videos          Video[]
  imagePosts      ImagePost[]

  createdAt       DateTime @default(now())
  updatedAt       DateTime @updatedAt
}

model Video {
  aweme_id       String   @id
  desc           String
  preview_title  String?
  duration_ms    Int
  created_at     DateTime
  share_url      String

  digg_count     BigInt   @default(0)
  comment_count  BigInt   @default(0)
  share_count    BigInt   @default(0)
  collect_count  BigInt   @default(0)

  // 视频分辨率（用于前端预布局）
  width          Int?
  height         Int?

  // 视频帧率
  fps            Int?

  // 视频封面（首帧提取后上传到 MinIO 的外链）
  cover_url      String?

  authorId       String
  author         Author   @relation(fields: [authorId], references: [sec_uid])

  comments       Comment[]

  tags           String[] // 视频标签列表
  video_url      String   // 视频文件 URL

  // 保存完整的接口原始 JSON 数据（用于备份和后续分析）
  raw_json       Json?

  createdAt      DateTime @default(now())
  updatedAt      DateTime @updatedAt

  @@index([authorId])
  @@index([created_at])
}

model CommentUser {
  id         String   @id @default(cuid())
  nickname   String
  avatar_url String?

  // 以 (nickname, avatar_url) 近似去重；如果你从响应里拿到用户 uid，可以改为以 uid 作为主键
  @@unique([nickname, avatar_url])

  comments   Comment[]
  createdAt  DateTime @default(now())
  updatedAt  DateTime @updatedAt
}

model Comment {
  cid          String      @id
  text         String
  digg_count   BigInt      @default(0)
  created_at   DateTime

  // 可关联视频或图文中的一种
  videoId      String?
  video        Video?      @relation(fields: [videoId], references: [aweme_id])

  imagePostId  String?
  imagePost    ImagePost?  @relation(fields: [imagePostId], references: [aweme_id])

  userId       String
  user         CommentUser @relation(fields: [userId], references: [id])

  createdAt    DateTime    @default(now())
  updatedAt    DateTime    @updatedAt

  @@index([videoId, created_at])
  @@index([imagePostId, created_at])
}

// 图片作品（图文）
model ImagePost {
  aweme_id      String   @id
  desc          String
  created_at    DateTime
  share_url     String

  digg_count    BigInt   @default(0)
  comment_count BigInt   @default(0)
  share_count   BigInt   @default(0)
  collect_count BigInt   @default(0)

  authorId      String
  author        Author   @relation(fields: [authorId], references: [sec_uid])

  tags          String[]
  music_url     String?  // 背景音乐（已上传到 MinIO 的外链）

  images        ImageFile[]
  comments      Comment[]

  // 保存完整的接口原始 JSON 数据（用于备份和后续分析）
  raw_json      Json?

  createdAt     DateTime @default(now())
  updatedAt     DateTime @updatedAt

  @@index([authorId])
  @@index([created_at])
}

// 图片作品中的单张图片（已上传到 MinIO 的外链）
model ImageFile {
  id        String    @id @default(cuid())
  postId    String
  post      ImagePost @relation(fields: [postId], references: [aweme_id])
  url       String
  order     Int       // 在作品中的顺序（从 0 开始）
  width     Int?
  height    Int?

  animated  String?   // 如果是动图，存储 video 格式的 URL

  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt

  @@index([postId, order])
  @@unique([postId, order])
}
