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

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

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

model Session {
  id            Int      @id @default(autoincrement())
  dcId          Int?
  port          Int?
  serverAddress String?
  authKey       Bytes?
  entities      Entity[]
}

model Entity {
  id        Int     @id @default(autoincrement())
  // 源代码里面大概支持 string 和 BigInteger，不如先全都存 String
  entityId  String
  sessionId Int
  session   Session @relation(fields: [sessionId], references: [id], onDelete: Cascade)
  hash      String?
  username  String?
  phone     String?
  name      String?

  @@unique([entityId, sessionId])
}

model Instance {
  id            Int           @id @default(autoincrement())
  owner         BigInt        @default(0)
  workMode      String        @default("")
  isSetup       Boolean       @default(false)
  Message       Message[]
  ForwardPair   ForwardPair[]
  botSessionId  Int?
  userSessionId Int?
  qqBotId       Int?
  qqBot         QqBot?        @relation(fields: [qqBotId], references: [id], onDelete: Cascade)
  reportUrl     String?
}

model QqBot {
  id       Int        @id @default(autoincrement())
  uin      BigInt     @default(0)
  password String     @default("")
  platform Int        @default(0)
  Instance Instance[]
}

model Message {
  id            Int      @id @default(autoincrement())
  qqRoomId      BigInt   @db.BigInt
  qqSenderId    BigInt   @db.BigInt
  time          Int
  brief         String?
  seq           Int
  rand          BigInt   @db.BigInt
  pktnum        Int
  tgChatId      BigInt   @db.BigInt
  tgMsgId       Int
  instanceId    Int      @default(0)
  instance      Instance @relation(fields: [instanceId], references: [id], onDelete: Cascade)
  tgFileId      BigInt?  @db.BigInt
  tgMessageText String?
  nick          String? // /抱 的时候会用到
  tgSenderId    BigInt?  @db.BigInt

  @@index([qqRoomId, qqSenderId, seq, rand, pktnum, time, instanceId])
  @@index([tgChatId, tgMsgId, instanceId])
}

model ForwardPair {
  id          Int           @id @default(autoincrement())
  qqRoomId    BigInt        @db.BigInt
  tgChatId    BigInt        @db.BigInt
  avatarCache AvatarCache[]
  instanceId  Int           @default(0)
  instance    Instance      @relation(fields: [instanceId], references: [id], onDelete: Cascade)
  joinNotice  Boolean       @default(true)
  poke        Boolean       @default(true)
  enable      Boolean       @default(true)
  disableQ2TG Boolean       @default(false)
  disableTG2Q Boolean       @default(false)

  @@unique([qqRoomId, instanceId])
  @@unique([tgChatId, instanceId])
}

model File {
  id     Int    @id @default(autoincrement())
  roomId BigInt @db.BigInt
  fileId String
  info   String
}

model FlashPhoto {
  id       Int              @id @default(autoincrement())
  photoMd5 String
  views    FlashPhotoView[]
}

model FlashPhotoView {
  id           Int        @id @default(autoincrement())
  flashPhotoId Int
  flashPhoto   FlashPhoto @relation(fields: [flashPhotoId], references: [id])
  viewerId     BigInt     @db.BigInt

  @@unique([flashPhotoId, viewerId])
}

model AvatarCache {
  id            Int         @id @default(autoincrement())
  forwardPair   ForwardPair @relation(fields: [forwardPairId], references: [id], onDelete: Cascade)
  forwardPairId Int         @unique
  hash          Bytes
}
