// IA Gen Maturity Evaluator - Prisma Schema
// SQLite for local dev; switch to Postgres for production

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

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

model User {
  id              String           @id @default(cuid())
  email           String           @unique
  name            String?
  passwordHash    String?
  role            String           @default("evaluator") // evaluator | admin
  createdAt       DateTime         @default(now())
  updatedAt       DateTime         @updatedAt
  evaluations     Evaluation[]     @relation("Evaluator")
  sharedEvaluations EvaluationShare[]
}

model Template {
  id          String   @id // "full-15"
  name        String
  dimensions  TemplateDimension[]
  evaluations Evaluation[]
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
}

model TemplateDimension {
  id             String   @id @default(cuid())
  templateId     String
  template       Template @relation(fields: [templateId], references: [id], onDelete: Cascade)
  slug             String   // "tools", "prompts", etc.
  orderIndex       Int
  title            String
  rubric           String   // "1:X;2:Y;3:Z;4:A;5:B" or "1-5"
  suggestedQuestions String? // JSON array: ["Q1", "Q2", "Q3"]
  dimensionScores  DimensionScore[]

  @@unique([templateId, slug])
}

model Evaluation {
  id              String   @id @default(cuid())
  candidateName   String
  candidateRole   String
  candidateTeam   String?  // équipe du candidat
  evaluatorName   String
  evaluatorId     String?
  evaluator       User?    @relation("Evaluator", fields: [evaluatorId], references: [id], onDelete: SetNull)
  evaluationDate  DateTime
  templateId      String
  template        Template @relation(fields: [templateId], references: [id])
  status          String   @default("draft") // draft | submitted
  findings        String?  // auto-generated summary
  recommendations String?
  dimensionScores DimensionScore[]
  auditLogs       AuditLog[]
  sharedWith      EvaluationShare[]
  isPublic        Boolean  @default(false) // visible par tous (ex. démo)
  createdAt       DateTime @default(now())
  updatedAt       DateTime @updatedAt

  @@index([evaluatorId])
  @@index([templateId])
}

model EvaluationShare {
  id           String     @id @default(cuid())
  evaluationId String
  evaluation   Evaluation @relation(fields: [evaluationId], references: [id], onDelete: Cascade)
  userId       String
  user         User       @relation(fields: [userId], references: [id], onDelete: Cascade)
  createdAt    DateTime   @default(now())

  @@unique([evaluationId, userId])
  @@index([userId])
}

model DimensionScore {
  id             String   @id @default(cuid())
  evaluationId   String
  evaluation     Evaluation @relation(fields: [evaluationId], references: [id], onDelete: Cascade)
  dimensionId    String
  dimension      TemplateDimension @relation(fields: [dimensionId], references: [id], onDelete: Cascade)
  score          Int?     // 1-5
  justification  String?
  examplesObserved String?
  confidence     String?  // low | med | high
  candidateNotes String?  // evaluator's notes from interview
  createdAt      DateTime @default(now())
  updatedAt      DateTime @updatedAt

  @@unique([evaluationId, dimensionId])
}

model AuditLog {
  id           String   @id @default(cuid())
  evaluationId String
  evaluation   Evaluation @relation(fields: [evaluationId], references: [id], onDelete: Cascade)
  action       String   // created | updated | submitted | score_changed
  field        String?
  oldValue     String?
  newValue     String?
  userId       String?
  createdAt    DateTime @default(now())

  @@index([evaluationId])
}
