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

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

model Form {
  id          Int        @id @default(autoincrement())
  title       String
  description String?
  createdAt   DateTime   @default(now()) @map("created_at")
  categories  Category[]
  responses   Response[]
}

model Category {
  id        Int     @id @default(autoincrement())
  formId    Int     @map("form_id")
  name      String
  sortOrder Int     @default(0) @map("sort_order")
  form      Form    @relation(fields: [formId], references: [id], onDelete: Cascade)
  fields    Field[]
}

enum FieldType {
  text
  number
  date
  textarea
  boolean
}

model Field {
  id             Int             @id @default(autoincrement())
  categoryId     Int             @map("category_id")
  label          String
  type           FieldType
  description    String?
  sortOrder      Int             @map("sort_order")
  category       Category        @relation(fields: [categoryId], references: [id], onDelete: Cascade)
  responseValues ResponseValue[]
}

model Response {
  id             Int             @id @default(autoincrement())
  formId         Int             @map("form_id")
  submittedAt    DateTime        @default(now()) @map("submitted_at")
  form           Form            @relation(fields: [formId], references: [id], onDelete: Cascade)
  responseValues ResponseValue[]
}

model ResponseValue {
  id         Int      @id @default(autoincrement())
  responseId Int      @map("response_id")
  fieldId    Int      @map("field_id")
  value      String?
  response   Response @relation(fields: [responseId], references: [id], onDelete: Cascade)
  field      Field    @relation(fields: [fieldId], references: [id], onDelete: Cascade)
}

model MantisSummary {
  id          Int      @id @default(autoincrement())
  summaryDate DateTime @unique @map("summary_date") @db.Date
  summaryText String   @map("summary_text")
  generatedAt DateTime @default(now()) @map("generated_at")
}

model Setting {
  key   String @id
  value String
}

model User {
  id             String           @id @default(uuid())
  username       String           @unique @db.Citext()
  authenticators Authenticator[]
  preferences    UserPreference[]

  email    String? @unique @db.Citext()
  fullName String?
}

model UserPreference {
  id     Int    @id @default(autoincrement())
  userId String @map("user_id")
  key    String
  value  String
  user   User   @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([userId, key])
}

model Authenticator {
  id                   String  @id @default(uuid())
  credentialID         String  @unique @map("credential_id")
  credentialPublicKey  Bytes   @map("credential_public_key")
  counter              BigInt
  credentialDeviceType String  @map("credential_device_type")
  credentialBackedUp   Boolean @map("credential_backed_up")
  transports           String?

  userId String @map("user_id")
  user   User   @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model ChatThread {
  id        String        @id @default(uuid())
  createdAt DateTime      @default(now()) @map("created_at")
  updatedAt DateTime      @updatedAt @map("updated_at")
  messages  ChatMessage[]
}

model ChatMessage {
  id        String     @id @default(uuid())
  threadId  String     @map("thread_id")
  sender    String // 'user' or 'bot'
  content   String
  createdAt DateTime   @default(now()) @map("created_at")
  thread    ChatThread @relation(fields: [threadId], references: [id], onDelete: Cascade)
}

// Add this model for session storage
model Session {
  id        String   @id
  sid       String   @unique
  data      String
  expiresAt DateTime @map("expires_at")
}
