// NDPA compliance schema — generated by create-ndpr for {{ORG_NAME_COMMENT}}
// Every row is tenant-scoped. Derive tenantId only from verified server context.

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

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

model ConsentRecord {
  id               String   @id @default(cuid())
  tenantId         String
  subjectId        String
  activeSubjectKey String?  @unique
  consents         Json
  version          String
  method           String
  hasInteracted    Boolean
  lawfulBasis      String?
  ipAddress        String?
  userAgent        String?
  clientTimestamp  DateTime
  createdAt        DateTime @default(now())
  revokedAt        DateTime?

  @@index([tenantId, subjectId, revokedAt])
  @@index([tenantId, subjectId, clientTimestamp, version, method])
  @@map("ndpr_consent_records")
}

model DSRRequest {
  id              String   @id @default(cuid())
  tenantId        String
  subjectId       String
  type            String
  status          String   @default("pending")
  subjectName     String
  subjectEmail    String
  subjectPhone    String?
  identifierType  String
  identifierValue String
  description     String?
  internalNotes   String?
  assignedTo      String?
  submittedAt     DateTime @default(now())
  acknowledgedAt  DateTime?
  completedAt     DateTime?
  dueAt           DateTime

  @@index([tenantId, status])
  @@index([tenantId, subjectId])
  @@index([tenantId, subjectEmail])
  @@map("ndpr_dsr_requests")
}

model BreachReport {
  id                     String   @id @default(cuid())
  tenantId               String
  title                  String
  description            String
  category               String
  severity               String
  status                 String   @default("ongoing")
  discoveredAt           DateTime
  occurredAt             DateTime?
  reportedAt             DateTime @default(now())
  ndpcNotifiedAt         DateTime?
  reporterName           String
  reporterEmail          String
  reporterDepartment     String?
  affectedSystems        Json
  dataTypes              Json
  involvesSensitiveData  Boolean  @default(false)
  estimatedAffected      Int?
  approximateRecordCount Int?
  dataSubjectCategories  Json
  likelyConsequences     String?
  mitigationMeasures     String?
  isPhasedReport         Boolean  @default(false)
  supplementsReportId    String?
  initialActions         String?
  ndpcNotificationSent   Boolean  @default(false)

  @@index([tenantId, status])
  @@index([tenantId, severity])
  @@map("ndpr_breach_reports")
}

model ProcessingRecord {
  id                String   @id @default(cuid())
  tenantId          String
  purpose           String
  lawfulBasis       String
  dataCategories    Json
  dataSubjects      Json
  recipients        Json
  retentionPeriod   String
  securityMeasures  Json
  transferCountries Json?
  transferMechanism String?
  dpiaConducted     Boolean  @default(false)
  status            String   @default("active")
  createdAt         DateTime @default(now())
  updatedAt         DateTime @updatedAt

  @@index([tenantId, status])
  @@map("ndpr_processing_records")
}

model DPIARecord {
  id          String    @id @default(cuid())
  tenantId    String
  projectName String
  description String
  dpiaData    Json
  overallRisk String
  score       Int
  status      String    @default("draft")
  conductedBy String
  approvedBy  String?
  createdAt   DateTime  @default(now())
  updatedAt   DateTime  @updatedAt
  removedAt   DateTime?

  @@index([tenantId, removedAt, status])
  @@map("ndpr_dpia_records")
}

model LawfulBasisRecord {
  id             String    @id @default(cuid())
  tenantId       String
  activityName   String
  lawfulBasis    String
  justification  String
  dataCategories Json
  purposes       Json
  assessedBy     String
  assessedAt     DateTime  @default(now())
  reviewDate     DateTime?
  createdAt      DateTime  @default(now())
  updatedAt      DateTime  @updatedAt
  removedAt      DateTime?

  @@index([tenantId, removedAt, lawfulBasis])
  @@map("ndpr_lawful_basis_records")
}

model CrossBorderTransferRecord {
  id                    String    @id @default(cuid())
  tenantId              String
  destinationCountry    String
  recipientName         String
  transferMechanism     String
  safeguards            String
  dataCategories        Json
  adequacyStatus        String
  ndpcApprovalRequired  Boolean   @default(false)
  ndpcApprovalReference String?
  riskLevel             String
  status                String    @default("active")
  createdAt             DateTime  @default(now())
  updatedAt             DateTime  @updatedAt
  removedAt             DateTime?

  @@index([tenantId, removedAt, destinationCountry])
  @@index([tenantId, removedAt, riskLevel])
  @@index([tenantId, removedAt, status])
  @@map("ndpr_cross_border_transfer_records")
}

model ComplianceAuditLog {
  id          String   @id @default(cuid())
  tenantId    String
  module      String
  action      String
  entityId    String
  entityType  String
  changes     Json?
  performedBy String?
  createdAt   DateTime @default(now())

  @@index([tenantId, module, entityId])
  @@map("ndpr_audit_log")
}
