/**
 * Wire shapes for the EMR Assistant (chatbot + document extraction).
 * Backend contract under /api/v2/emr-assistant/*.
 */

import type { EmrSection } from './emr-types';

export type AssistantRole = 'user' | 'assistant' | 'system';

export interface ProposedUpdate {
  section: EmrSection;
  /**
   * Free-shaped payload the backend wants written into the EMR section.
   * The UI renders it as editable key/value pairs and posts back via
   * `/apply` exactly as confirmed by the patient.
   */
  payload: Record<string, unknown>;
}

export interface AssistantMessage {
  role: AssistantRole;
  content: string;
  proposedUpdates?: ProposedUpdate;
  suggestedReplies?: string[];
}

export interface ConversationTurn {
  conversationId: string;
  message: AssistantMessage;
}

export interface ConversationHistory {
  id: string;
  locale: string;
  messages: AssistantMessage[];
}

export interface ApplyResponse {
  applied: true;
}

// ---------- Document extraction ----------

export type EmrDocumentType =
  | 'lab_report'
  | 'prescription'
  | 'discharge_summary'
  | 'vaccination_card'
  | 'imaging_report'
  | 'other';

export interface DocumentMedication {
  name: string;
  dosage?: string;
  frequency?: string;
  notes?: string;
}

export interface DocumentCondition {
  name: string;
  icd?: string;
  notes?: string;
}

export interface DocumentAllergy {
  substance: string;
  reaction?: string;
  severity?: string;
}

export interface DocumentVital {
  name: string;
  value: string;
  unit?: string;
  takenAt?: string;
}

export interface DocumentImmunization {
  name: string;
  date?: string;
  notes?: string;
}

export type ExtractionStatus = 'ready' | 'processing' | 'failed';

export interface DocumentExtraction {
  extractionId: string;
  status: ExtractionStatus;
  medications?: DocumentMedication[];
  conditions?: DocumentCondition[];
  allergies?: DocumentAllergy[];
  vitals?: DocumentVital[];
  immunizations?: DocumentImmunization[];
  rawText?: string;
  confidence?: number;
}

/**
 * Caller-built map of which items the patient accepted, indexed by
 * category. Posted to `/documents/apply` as `accepted`.
 */
export interface ExtractionAcceptance {
  medications?: DocumentMedication[];
  conditions?: DocumentCondition[];
  allergies?: DocumentAllergy[];
  vitals?: DocumentVital[];
  immunizations?: DocumentImmunization[];
}

export const DOCUMENT_TYPE_OPTIONS: Array<{ value: EmrDocumentType; label: string }> = [
  { value: 'lab_report', label: 'Lab report' },
  { value: 'prescription', label: 'Prescription' },
  { value: 'discharge_summary', label: 'Discharge summary' },
  { value: 'vaccination_card', label: 'Vaccination card' },
  { value: 'imaging_report', label: 'Imaging / radiology' },
  { value: 'other', label: 'Other' },
];
