-- Baltique OS Core 2.7.2
-- Governed supplier routing / automatic e-mail ordering / patient AI questions.
-- This migration creates schema only. Runtime settings remain safe by default:
-- automatic approval, automatic supplier e-mail and external AI are OFF.

CREATE TABLE IF NOT EXISTS enterprise_procurement_suppliers (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      supplier_key VARCHAR(140) NOT NULL,
      display_name VARCHAR(255) NOT NULL,
      legal_name VARCHAR(255) NULL,
      supplier_type VARCHAR(80) NOT NULL DEFAULT 'other',
      tax_id VARCHAR(80) NULL,
      registration_no VARCHAR(120) NULL,
      address_json JSON NULL,
      locale ENUM('pl','en') NOT NULL DEFAULT 'pl',
      default_currency CHAR(3) NOT NULL DEFAULT 'PLN',
      email_to VARCHAR(500) NULL,
      email_cc VARCHAR(1000) NULL,
      email_bcc VARCHAR(1000) NULL,
      reply_to VARCHAR(255) NULL,
      subject_template VARCHAR(500) NULL,
      body_template LONGTEXT NULL,
      allowed_sites_json JSON NULL,
      allowed_domains_json JSON NULL,
      approval_status ENUM('draft','approved','suspended','archived') NOT NULL DEFAULT 'draft',
      approved_by BIGINT NULL,
      approved_at DATETIME NULL,
      verified_by BIGINT NULL,
      verified_at DATETIME NULL,
      active TINYINT(1) NOT NULL DEFAULT 1,
      auto_email_enabled TINYINT(1) NOT NULL DEFAULT 0,
      require_manual_approval TINYINT(1) NOT NULL DEFAULT 1,
      max_auto_order_value DECIMAL(16,2) NULL,
      minimum_order_value DECIMAL(16,2) NULL,
      default_lead_time_days INT UNSIGNED NULL,
      metadata_json JSON NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY(id), UNIQUE KEY uq_proc_supplier_key(supplier_key),
      KEY idx_proc_supplier_type(supplier_type,active), KEY idx_proc_supplier_approval(approval_status,active)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_procurement_supplier_contacts (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      supplier_id BIGINT UNSIGNED NOT NULL,
      contact_type ENUM('orders','finance','quality','returns','emergency','other') NOT NULL DEFAULT 'orders',
      contact_name VARCHAR(255) NULL,
      email VARCHAR(255) NULL,
      phone VARCHAR(80) NULL,
      primary_flag TINYINT(1) NOT NULL DEFAULT 0,
      active TINYINT(1) NOT NULL DEFAULT 1,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY(id), KEY idx_proc_supplier_contact(supplier_id,contact_type,active)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_procurement_item_mappings (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      supplier_id BIGINT UNSIGNED NOT NULL,
      source_type ENUM('supply_item','sterile_set','service','other') NOT NULL DEFAULT 'supply_item',
      source_id BIGINT UNSIGNED NULL,
      source_key VARCHAR(190) NULL,
      supplier_sku VARCHAR(190) NULL,
      manufacturer_sku VARCHAR(190) NULL,
      package_label VARCHAR(255) NULL,
      pack_quantity DECIMAL(16,3) NOT NULL DEFAULT 1,
      unit_price DECIMAL(16,4) NULL,
      currency CHAR(3) NOT NULL DEFAULT 'PLN',
      minimum_order_qty DECIMAL(16,3) NULL,
      order_multiple DECIMAL(16,3) NULL,
      lead_time_days INT UNSIGNED NULL,
      preferred TINYINT(1) NOT NULL DEFAULT 0,
      approval_status ENUM('draft','approved','suspended','archived') NOT NULL DEFAULT 'draft',
      approved_by BIGINT NULL,
      approved_at DATETIME NULL,
      active TINYINT(1) NOT NULL DEFAULT 1,
      metadata_json JSON NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY(id),
      UNIQUE KEY uq_proc_mapping_supplier_source(supplier_id,source_type,source_id,source_key),
      KEY idx_proc_mapping_source(source_type,source_id,active), KEY idx_proc_mapping_preferred(preferred,approval_status,active)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_procurement_routing_rules (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      rule_key VARCHAR(190) NOT NULL,
      site_id BIGINT NULL,
      source_domain VARCHAR(80) NOT NULL DEFAULT 'all',
      item_type VARCHAR(80) NULL,
      category_key VARCHAR(120) NULL,
      supplier_id BIGINT UNSIGNED NOT NULL,
      auto_create_order TINYINT(1) NOT NULL DEFAULT 1,
      auto_queue_email TINYINT(1) NOT NULL DEFAULT 0,
      require_manual_approval TINYINT(1) NOT NULL DEFAULT 1,
      max_auto_value DECIMAL(16,2) NULL,
      send_window_start TIME NULL,
      send_window_end TIME NULL,
      weekdays_json JSON NULL,
      active TINYINT(1) NOT NULL DEFAULT 1,
      sort_order INT NOT NULL DEFAULT 100,
      created_by BIGINT NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY(id), UNIQUE KEY uq_proc_routing_rule(rule_key),
      KEY idx_proc_routing_lookup(site_id,source_domain,item_type,category_key,active,sort_order)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_procurement_requests (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      request_key VARCHAR(160) NOT NULL,
      site_id BIGINT NULL,
      source_domain VARCHAR(80) NOT NULL DEFAULT 'other',
      source_table VARCHAR(120) NULL,
      source_id BIGINT NULL,
      title VARCHAR(255) NOT NULL,
      reason TEXT NULL,
      priority ENUM('low','normal','high','urgent','critical') NOT NULL DEFAULT 'normal',
      status ENUM('draft','submitted','reviewed','approved','routed','partially_routed','ordered','received','closed','cancelled','rejected') NOT NULL DEFAULT 'draft',
      requested_by BIGINT NULL,
      reviewed_by BIGINT NULL,
      approved_by BIGINT NULL,
      submitted_at DATETIME NULL,
      approved_at DATETIME NULL,
      notes TEXT NULL,
      metadata_json JSON NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY(id), UNIQUE KEY uq_proc_request_key(request_key),
      UNIQUE KEY uq_proc_request_source(source_table,source_id),
      KEY idx_proc_request_status(status,priority), KEY idx_proc_request_site(site_id,status)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_procurement_request_items (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      request_id BIGINT UNSIGNED NOT NULL,
      source_type ENUM('supply_item','sterile_set','service','other') NOT NULL DEFAULT 'other',
      source_id BIGINT UNSIGNED NULL,
      source_key VARCHAR(190) NULL,
      description VARCHAR(500) NOT NULL,
      category_key VARCHAR(120) NULL,
      item_type VARCHAR(80) NULL,
      quantity DECIMAL(16,3) NOT NULL DEFAULT 1,
      unit_label VARCHAR(80) NOT NULL DEFAULT 'szt.',
      estimated_unit_price DECIMAL(16,4) NULL,
      currency CHAR(3) NOT NULL DEFAULT 'PLN',
      preferred_supplier_id BIGINT UNSIGNED NULL,
      delivery_location_id BIGINT UNSIGNED NULL,
      required_by DATE NULL,
      route_status ENUM('pending','routed','blocked','excluded') NOT NULL DEFAULT 'pending',
      route_message TEXT NULL,
      notes TEXT NULL,
      metadata_json JSON NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY(id), KEY idx_proc_request_item_request(request_id,route_status),
      KEY idx_proc_request_item_source(source_type,source_id), KEY idx_proc_request_item_supplier(preferred_supplier_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_purchase_order_items (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      purchase_order_id BIGINT UNSIGNED NOT NULL,
      request_item_id BIGINT UNSIGNED NOT NULL,
      source_type VARCHAR(40) NOT NULL,
      source_id BIGINT UNSIGNED NULL,
      description VARCHAR(500) NOT NULL,
      supplier_sku VARCHAR(190) NULL,
      quantity DECIMAL(16,3) NOT NULL,
      unit_label VARCHAR(80) NOT NULL DEFAULT 'szt.',
      pack_quantity DECIMAL(16,3) NOT NULL DEFAULT 1,
      unit_price DECIMAL(16,4) NULL,
      currency CHAR(3) NOT NULL DEFAULT 'PLN',
      line_total DECIMAL(16,2) NULL,
      received_quantity DECIMAL(16,3) NOT NULL DEFAULT 0,
      required_by DATE NULL,
      notes TEXT NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY(id), UNIQUE KEY uq_purchase_order_request_item(purchase_order_id,request_item_id),
      KEY idx_purchase_order_item_order(purchase_order_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_purchase_order_dispatches (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      purchase_order_id BIGINT UNSIGNED NOT NULL,
      dispatch_uid VARCHAR(80) NOT NULL,
      snapshot_sha256 CHAR(64) NOT NULL,
      idempotency_key CHAR(64) NOT NULL,
      recipient_email VARCHAR(255) NOT NULL,
      cc_emails TEXT NULL,
      subject VARCHAR(500) NOT NULL,
      status ENUM('prepared','queued','sent','failed','cancelled') NOT NULL DEFAULT 'prepared',
      automatic_flag TINYINT(1) NOT NULL DEFAULT 0,
      mail_queue_id BIGINT UNSIGNED NULL,
      queued_by BIGINT NULL,
      queued_at DATETIME NULL,
      sent_at DATETIME NULL,
      last_error TEXT NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY(id), UNIQUE KEY uq_proc_dispatch_uid(dispatch_uid), UNIQUE KEY uq_proc_dispatch_idempotency(idempotency_key),
      KEY idx_proc_dispatch_order(purchase_order_id,status), KEY idx_proc_dispatch_mail(mail_queue_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_purchase_order_events (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      purchase_order_id BIGINT UNSIGNED NOT NULL,
      event_type VARCHAR(80) NOT NULL,
      from_status VARCHAR(40) NULL,
      to_status VARCHAR(40) NULL,
      actor_user_id BIGINT NULL,
      details_json JSON NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY(id), KEY idx_purchase_order_event(purchase_order_id,created_at)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_supplier_acknowledgements (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      purchase_order_id BIGINT UNSIGNED NOT NULL,
      acknowledgement_uid VARCHAR(80) NOT NULL,
      response_status ENUM('accepted','partially_available','rejected','needs_clarification') NOT NULL,
      supplier_reference VARCHAR(255) NULL,
      expected_delivery_date DATE NULL,
      contact_name VARCHAR(255) NULL,
      contact_email VARCHAR(255) NULL,
      message TEXT NULL,
      source_ip VARCHAR(64) NULL,
      user_agent VARCHAR(500) NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY(id), UNIQUE KEY uq_supplier_ack_uid(acknowledgement_uid),
      KEY idx_supplier_ack_order(purchase_order_id,created_at)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_procurement_runs (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      run_uid VARCHAR(80) NOT NULL,
      run_type VARCHAR(80) NOT NULL,
      status VARCHAR(40) NOT NULL DEFAULT 'running',
      scanned_count INT UNSIGNED NOT NULL DEFAULT 0,
      created_count INT UNSIGNED NOT NULL DEFAULT 0,
      queued_count INT UNSIGNED NOT NULL DEFAULT 0,
      failed_count INT UNSIGNED NOT NULL DEFAULT 0,
      details_json JSON NULL,
      created_by BIGINT NULL,
      started_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      finished_at DATETIME NULL,
      PRIMARY KEY(id), UNIQUE KEY uq_proc_run_uid(run_uid), KEY idx_proc_run_type(run_type,started_at)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_patient_ai_knowledge_items (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      knowledge_key VARCHAR(190) NOT NULL,
      locale ENUM('pl','en') NOT NULL DEFAULT 'pl',
      category_key VARCHAR(120) NOT NULL DEFAULT 'general',
      title VARCHAR(255) NOT NULL,
      match_terms TEXT NOT NULL,
      approved_text LONGTEXT NOT NULL,
      risk_class ENUM('administrative','educational','clinical_general','clinical_individualized','emergency') NOT NULL DEFAULT 'administrative',
      target_role VARCHAR(80) NULL,
      auto_answer_allowed TINYINT(1) NOT NULL DEFAULT 0,
      external_ai_allowed TINYINT(1) NOT NULL DEFAULT 0,
      source_type VARCHAR(80) NOT NULL DEFAULT 'manual',
      source_table VARCHAR(120) NULL,
      source_id BIGINT NULL,
      source_version VARCHAR(80) NULL,
      source_sha256 CHAR(64) NULL,
      approval_status ENUM('draft','approved','suspended','archived') NOT NULL DEFAULT 'draft',
      approved_by BIGINT NULL,
      approved_at DATETIME NULL,
      valid_from DATETIME NULL,
      valid_until DATETIME NULL,
      active TINYINT(1) NOT NULL DEFAULT 1,
      created_by BIGINT NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY(id),
      UNIQUE KEY uq_patient_ai_knowledge(knowledge_key,locale),
      KEY idx_patient_ai_knowledge_lookup(locale,approval_status,active,category_key),
      KEY idx_patient_ai_knowledge_risk(risk_class,auto_answer_allowed)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_patient_ai_messages (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      conversation_id BIGINT UNSIGNED NOT NULL,
      patient_id BIGINT UNSIGNED NOT NULL,
      message_role ENUM('patient','assistant','staff','system') NOT NULL,
      message_text LONGTEXT NOT NULL,
      message_sha256 CHAR(64) NOT NULL,
      locale ENUM('pl','en') NOT NULL DEFAULT 'pl',
      risk_class VARCHAR(80) NULL,
      category_key VARCHAR(120) NULL,
      answer_mode ENUM('patient_question','approved_exact','approved_ai_synthesis','safe_escalation','staff_reply','system_notice') NOT NULL DEFAULT 'patient_question',
      confidence DECIMAL(6,5) NULL,
      knowledge_ids_json JSON NULL,
      provider VARCHAR(80) NULL,
      model VARCHAR(120) NULL,
      provider_run_id BIGINT UNSIGNED NULL,
      external_processing_consent TINYINT(1) NOT NULL DEFAULT 0,
      review_status ENUM('not_required','pending','approved','rejected') NOT NULL DEFAULT 'not_required',
      visible_to_patient TINYINT(1) NOT NULL DEFAULT 1,
      created_by BIGINT NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY(id),
      KEY idx_patient_ai_message_conversation(conversation_id,id),
      KEY idx_patient_ai_message_patient(patient_id,created_at),
      KEY idx_patient_ai_message_review(review_status,created_at)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_patient_ai_escalations (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      escalation_uid VARCHAR(80) NOT NULL,
      conversation_id BIGINT UNSIGNED NOT NULL,
      patient_message_id BIGINT UNSIGNED NOT NULL,
      patient_id BIGINT UNSIGNED NOT NULL,
      operative_case_id BIGINT UNSIGNED NULL,
      target_role VARCHAR(80) NOT NULL DEFAULT 'coordinator',
      priority ENUM('normal','high','urgent','critical') NOT NULL DEFAULT 'normal',
      reason_code VARCHAR(120) NOT NULL,
      reason_text TEXT NOT NULL,
      status ENUM('open','in_progress','resolved','dismissed') NOT NULL DEFAULT 'open',
      work_item_id BIGINT UNSIGNED NULL,
      assigned_user_id BIGINT UNSIGNED NULL,
      resolved_by BIGINT UNSIGNED NULL,
      resolution_note TEXT NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      resolved_at DATETIME NULL,
      PRIMARY KEY(id), UNIQUE KEY uq_patient_ai_escalation_uid(escalation_uid),
      UNIQUE KEY uq_patient_ai_escalation_message(patient_message_id),
      KEY idx_patient_ai_escalation_inbox(status,target_role,priority,created_at)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_patient_ai_provider_runs (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      request_uid VARCHAR(80) NOT NULL,
      conversation_id BIGINT UNSIGNED NULL,
      patient_message_id BIGINT UNSIGNED NULL,
      provider VARCHAR(80) NOT NULL DEFAULT 'local',
      model VARCHAR(120) NULL,
      status ENUM('started','completed','failed','blocked','skipped') NOT NULL DEFAULT 'started',
      latency_ms INT UNSIGNED NULL,
      prompt_sha256 CHAR(64) NULL,
      response_sha256 CHAR(64) NULL,
      input_chars INT UNSIGNED NOT NULL DEFAULT 0,
      output_chars INT UNSIGNED NOT NULL DEFAULT 0,
      details_json JSON NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      finished_at DATETIME NULL,
      PRIMARY KEY(id), UNIQUE KEY uq_patient_ai_provider_request(request_uid),
      KEY idx_patient_ai_provider_status(provider,status,created_at)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_patient_ai_feedback (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      conversation_id BIGINT UNSIGNED NOT NULL,
      message_id BIGINT UNSIGNED NOT NULL,
      patient_id BIGINT UNSIGNED NOT NULL,
      helpful TINYINT(1) NULL,
      feedback_text TEXT NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY(id), UNIQUE KEY uq_patient_ai_feedback_message(message_id,patient_id),
      KEY idx_patient_ai_feedback_conversation(conversation_id)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_patient_ai_events (
      id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
      conversation_id BIGINT UNSIGNED NULL,
      message_id BIGINT UNSIGNED NULL,
      event_type VARCHAR(120) NOT NULL,
      actor_user_id BIGINT UNSIGNED NULL,
      details_json JSON NULL,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY(id), KEY idx_patient_ai_event_conversation(conversation_id,created_at),
      KEY idx_patient_ai_event_type(event_type,created_at)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Safe defaults and version markers.
INSERT INTO app_settings(setting_key,setting_value,created_at,updated_at) VALUES
('enterprise_procurement_version','2.7.2',NOW(),NOW()),
('enterprise_procurement_email_enabled','1',NOW(),NOW()),
('enterprise_procurement_auto_materialize_enabled','1',NOW(),NOW()),
('enterprise_procurement_auto_approve_enabled','0',NOW(),NOW()),
('enterprise_procurement_auto_email_enabled','0',NOW(),NOW()),
('enterprise_procurement_cron_enabled','0',NOW(),NOW()),
('enterprise_procurement_no_patient_data_in_orders','1',NOW(),NOW()),
('enterprise_procurement_no_unapproved_supplier_send','1',NOW(),NOW()),
('enterprise_procurement_no_duplicate_dispatch','1',NOW(),NOW()),
('enterprise_procurement_stable_idempotency','1',NOW(),NOW()),
('enterprise_patient_ai_version','2.7.2',NOW(),NOW()),
('enterprise_patient_ai_enabled','1',NOW(),NOW()),
('enterprise_patient_ai_local_answers_enabled','1',NOW(),NOW()),
('enterprise_patient_ai_external_enabled','0',NOW(),NOW()),
('enterprise_patient_ai_external_consent_required','1',NOW(),NOW()),
('enterprise_patient_ai_auto_clinical_answer_enabled','0',NOW(),NOW()),
('enterprise_patient_ai_auto_medication_advice_enabled','0',NOW(),NOW()),
('enterprise_patient_ai_auto_diagnosis_enabled','0',NOW(),NOW()),
('enterprise_patient_ai_auto_go_hold_enabled','0',NOW(),NOW()),
('enterprise_patient_ai_emergency_auto_answer_enabled','0',NOW(),NOW()),
('core_version_272','1',NOW(),NOW()),
('enterprise_release_272_applied','1',NOW(),NOW())
ON DUPLICATE KEY UPDATE setting_value=VALUES(setting_value),updated_at=NOW();
