export interface User {
  id: string
  username: string
  fullName: string
  role: string
}

export interface LoginRequest {
  username: string
  password: string
}

export interface LoginResponse {
  token: string
  refreshToken: string
  userId: string
  username: string
  fullName: string
  role: string
}

export interface TokenResponse {
  token: string
  refreshToken: string
}

export interface ApiResponse<T> {
  success: boolean
  statusCode: number
  data: T | null
  error: ApiError | null
}

export interface ApiError {
  message: string
  code: string
}

export interface BatchTypeFieldRequirements {
  showPatientDemographics: boolean
  showEncounterContext: boolean
  showEncounterSummaryFields: boolean
  showObservations: boolean
  showAllergies: boolean
  showMedications: boolean
}

export interface SupersessionInfo {
  originalBatchId: string
  originalBatchStatus: string
  originalPromotedAt: string
  originalObservationCount: number
}

export interface BatchDetailResponse {
  id: string
  status: string
  batchType: string
  track: string
  fieldRequirements: BatchTypeFieldRequirements
  patientId: string | null
  documentRef: string
  documentUrl: string | null
  enableRetroactiveAlerts: boolean
  enteredByUserId: string | null
  verifiedByUserId: string | null
  approvedByUserId: string | null
  rejectionReason: string | null
  promotedAt: string | null
  promotionEncounterId: string | null
  supersedesBatchId: string | null
  clinicianAttestation: boolean
  isCorrection: boolean
  supersession: SupersessionInfo | null
  createdAt: string
  updatedAt: string
}

export interface BatchListResponse {
  items: BatchDetailResponse[]
  page: number
  pageSize: number
  totalCount: number
  totalPages: number
}

export interface UserProfileResponse {
  id: string
  username: string
  fullName: string
  role: string
}

export interface DraftPatient {
  id: string
  fullName: string | null
  dateOfBirth: string | null
  sex: string | null
  bloodType: string | null
  emergencyContact: string | null
  allergies: string[] | null
  noKnownAllergies: boolean
  medications: string[] | null
  noActiveMedications: boolean
  updatedAt?: string
}

export interface DraftEncounter {
  id: string
  batchId: string
  admissionDate: string | null
  department: string | null
  roomBed: string | null
  admissionReason: string | null
  dischargeDiagnosis: string | null
  status: string | null
}

export interface DraftObservation {
  id: string
  batchId: string
  observationCode: string
  value: number
  unit: string
  recordedAt: string
  note: string | null
}

export interface OcrConfidenceMap {
  provider: string
  processedAt: string
  durationMs: number
  fieldConfidences: Record<string, number>
}

export interface BatchDraft {
  batchId: string
  status: string
  batchType: string
  fieldRequirements: BatchTypeFieldRequirements
  ocrConfidence: OcrConfidenceMap | null
  patient: DraftPatient | null
  encounter: DraftEncounter | null
  observations: DraftObservation[]
}

export interface FieldCheck {
  fieldPath: string
  passed: boolean
}

export interface PagedResult<T> {
  items: T[]
  page: number
  pageSize: number
  totalCount: number
  totalPages: number
}

export interface WorkQueueOverview {
  statusCounts: Record<string, number>
  averageTimeInQueueMinutes: number
  rejectRate: number
  oldestPendingVerificationMinutes: number
}

export interface PatientSearchResult {
  id: string
  fullName: string
  mrn: string
}

export interface UserSummary {
  id: string
  username: string
  fullName: string
  role: string
}

export interface CoverSheetResponse {
  id: string
  code: string
  batchType: string
  track: string
  patientId: string | null
  patientName: string | null
  patientMrn: string | null
  assignToUserId: string | null
  assignToUserName: string | null
  isUsed: boolean
  batchId: string | null
  createdAt: string
  usedAt: string | null
}

export interface GenerateCoverSheetsRequest {
  count: number
  batchType: string
  track: string
  patientId?: string
  assignToUserId?: string
}

export interface DigitizationEventSummary {
  eventType: string
  occurredAt: string
  actorUserId: string
  actorName: string
  metadataJson: string | null
}

export interface DigitizationHistoryEntry {
  batchId: string
  status: string
  batchType: string
  track: string
  supersedesBatchId: string | null
  isCorrection: boolean
  hasBeenSuperseded: boolean
  supersededByBatchId: string | null
  draftObservationCount: number
  liveObservationCount: number
  supersededObservationCount: number
  createdAt: string
  promotedAt: string | null
  promotionEncounterId: string | null
  enteredByUserId: string | null
  enteredByUserName: string | null
  verifiedByUserId: string | null
  verifiedByUserName: string | null
  approvedByUserId: string | null
  approvedByUserName: string | null
  auditTrail: DigitizationEventSummary[]
}

export interface PatientDigitizationHistoryResponse {
  patientId: string
  totalBatches: number
  promotedBatches: number
  supersededBatches: number
  pendingBatches: number
  entries: DigitizationHistoryEntry[]
}

export interface LiveCaptureObservationInput {
  observationCode: string
  value: number | null
  unit: string
  recordedAt: string
  note: string
}

export interface LiveCaptureCriticalAlert {
  alertId: string
  severity: string
  message: string
  thresholdValue: number
  thresholdBound: string
}

export interface LiveCaptureObservationResponse {
  draftObservationId: string
  liveObservationId: string
  observationCode: string
  value: number
  unit: string
  recordedAt: string
  criticalAlert: LiveCaptureCriticalAlert | null
}

export interface LiveCaptureResponse {
  batchId: string
  encounterId: string
  observations: LiveCaptureObservationResponse[]
  criticalAlertCount: number
  promotedAt: string
}

export interface BatchTypeFieldRequirements {
  showPatientDemographics: boolean
  showEncounterContext: boolean
  showEncounterSummaryFields: boolean
  showObservations: boolean
  showAllergies: boolean
  showMedications: boolean
}