// =============================================================================
// Presence Service - Prisma Schema
// =============================================================================

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

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

// 事件日志表 (append-only)
model EventLog {
  id          BigInt   @id @default(autoincrement())
  userId      String?  @map("user_id") @db.VarChar(20)  // userSerialNum, e.g. "D25121400005"
  installId   String   @map("install_id") @db.VarChar(64)
  eventName   String   @map("event_name") @db.VarChar(64)
  eventTime   DateTime @map("event_time") @db.Timestamptz()
  deviceBrand String?  @map("device_brand") @db.VarChar(64)
  deviceModel String?  @map("device_model") @db.VarChar(64)
  deviceOs    String?  @map("device_os") @db.VarChar(32)
  appVersion  String?  @map("app_version") @db.VarChar(32)
  locale      String?  @map("locale") @db.VarChar(16)
  properties  Json?    @db.JsonB
  createdAt   DateTime @default(now()) @map("created_at") @db.Timestamptz()

  @@index([eventTime], name: "idx_event_log_event_time")
  @@index([eventName], name: "idx_event_log_event_name")
  @@index([eventName, eventTime], name: "idx_event_log_event_name_time")
  @@index([userId], name: "idx_event_log_user_id")
  @@index([deviceBrand], name: "idx_event_log_device_brand")
  @@index([appVersion], name: "idx_event_log_app_version")
  @@map("analytics_event_log")
}

// 设备档案表 (每台设备一行, upsert 更新)
// 大厂标准: 事件流水 + 设备快照分离, 设备分布查询直接走此表
model DeviceProfile {
  installId   String   @id @map("install_id") @db.VarChar(64)
  userId      String?  @map("user_id") @db.VarChar(20)
  deviceBrand String?  @map("device_brand") @db.VarChar(64)
  deviceModel String?  @map("device_model") @db.VarChar(64)
  deviceOs    String?  @map("device_os") @db.VarChar(32)
  appVersion  String?  @map("app_version") @db.VarChar(32)
  locale      String?  @map("locale") @db.VarChar(16)
  firstSeenAt DateTime @default(now()) @map("first_seen_at") @db.Timestamptz()
  lastSeenAt  DateTime @updatedAt @map("last_seen_at") @db.Timestamptz()
  eventCount  Int      @default(1) @map("event_count")

  @@index([deviceBrand], name: "idx_device_profile_brand")
  @@index([appVersion], name: "idx_device_profile_app_version")
  @@index([userId], name: "idx_device_profile_user_id")
  @@index([lastSeenAt(sort: Desc)], name: "idx_device_profile_last_seen")
  @@map("analytics_device_profile")
}

// 日活统计表
model DailyActiveStats {
  day           DateTime @id @map("day") @db.Date
  dauCount      Int      @map("dau_count")
  dauByProvince Json?    @map("dau_by_province") @db.JsonB
  dauByCity     Json?    @map("dau_by_city") @db.JsonB
  calculatedAt  DateTime @map("calculated_at") @db.Timestamptz()
  version       Int      @default(1)

  @@map("analytics_daily_active_users")
}

// 在线人数快照表
model OnlineSnapshot {
  id            BigInt   @id @default(autoincrement())
  ts            DateTime @unique @db.Timestamptz()
  onlineCount   Int      @map("online_count")
  windowSeconds Int      @default(180) @map("window_seconds")

  @@index([ts(sort: Desc)], name: "idx_online_snapshots_ts")
  @@map("analytics_online_snapshots")
}
