generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "linux-musl-openssl-3.0.x"]
}

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

model Experiment {
  id                    String   @id @default(uuid())
  title                 String
  created_at            DateTime @default(now())
  template              Json     @default("[]")
  subject_info_template Json     @default("[]")
  plot_config           Json?
  animals               Animal[]

  @@map("experiments")
}

model Animal {
  id                  String        @id @default(uuid())
  experiment_id       String
  animal_id_string    String
  animal_name         String
  subject_info        Json?
  csv_analysis_config Json?
  experiment          Experiment    @relation(fields: [experiment_id], references: [id], onDelete: Cascade)
  daily_statuses      DailyStatus[]

  @@map("animals")
}

model DailyStatus {
  id                     String          @id @default(uuid())
  animal_id              String
  date                   DateTime        @db.Date
  experiment_description String?
  vitals                 String?
  treatment              String?
  notes                  String?
  custom_fields          Json?
  analysis_summary       Json?
  animal                 Animal          @relation(fields: [animal_id], references: [id], onDelete: Cascade)
  analyses               DailyAnalysis[]
  session_files          SessionFile[]

  @@map("daily_statuses")
}

model SessionFile {
  id                    String      @id @default(uuid())
  daily_status_id       String
  daily_status          DailyStatus @relation(fields: [daily_status_id], references: [id], onDelete: Cascade)
  filename              String
  file_size             BigInt
  file_type             String?
  last_modified         BigInt?
  matched_identifier    String?     // the animal ID/name substring that matched in the filename
  animal_id_string_snap String?     // snapshot of animal.animal_id_string at registration time
  animal_name_snap      String?     // snapshot of animal.animal_name at registration time
  notes                 String?
  created_at            DateTime    @default(now())

  @@map("session_files")
}

model DailyAnalysis {
  id                String      @id @default(uuid())
  daily_status_id   String
  daily_status      DailyStatus @relation(fields: [daily_status_id], references: [id], onDelete: Cascade)
  created_at        DateTime    @default(now())
  file_name         String?
  timestamp_col     String
  note_col          String
  classifications   Json
  total_note_rows   Int
  session_end_ts    Float?
  sequence          Json
  category_counts   Json
  consecutive_stats Json
  runs              Json

  @@map("daily_analyses")
}

model AuditLog {
  id         String   @id @default(uuid())
  table_name String
  record_id  String
  action     String
  changes    Json
  timestamp  DateTime @default(now())

  @@map("audit_logs")
}
