-- Baltique OS Core 0.17.0 — Public Pre-Triage / CarePath Matching
-- Reference migration. Prefer /upgrade_0_17_0.php because it is idempotent.

CREATE TABLE IF NOT EXISTS public_pretriage_requests (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  public_token VARCHAR(128) NOT NULL,
  patient_id BIGINT UNSIGNED NULL,
  appointment_id BIGINT UNSIGNED NULL,
  status VARCHAR(40) NOT NULL DEFAULT 'created',
  contact_json JSON NULL,
  answers_json JSON NULL,
  result_json JSON NULL,
  suggested_procedure_key VARCHAR(160) NULL,
  suggested_procedure_label VARCHAR(190) NULL,
  suggested_procedure_category VARCHAR(80) NULL,
  suggested_consultation_type_id BIGINT UNSIGNED NULL,
  suggested_provider_id BIGINT UNSIGNED NULL,
  suggested_location_id BIGINT UNSIGNED NULL,
  suggested_procedure_template_id BIGINT UNSIGNED NULL,
  urgency VARCHAR(60) NOT NULL DEFAULT 'standard',
  priority VARCHAR(40) NOT NULL DEFAULT 'normal',
  carepath_applied_at DATETIME NULL,
  carepath_note TEXT NULL,
  ip_address VARCHAR(64) NULL,
  user_agent VARCHAR(500) NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_public_pretriage_token (public_token),
  KEY idx_public_pretriage_patient (patient_id),
  KEY idx_public_pretriage_appointment (appointment_id),
  KEY idx_public_pretriage_status (status, created_at),
  KEY idx_public_pretriage_priority (priority, created_at),
  KEY idx_public_pretriage_procedure (suggested_procedure_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
