-- Baltique OS Core 0.78.15 — Enterprise Finance & Payment Gate
-- Reference migration. Runtime upgrade /upgrade_0_78_15.php performs idempotent ALTER/seed/sync.

CREATE TABLE IF NOT EXISTS enterprise_finance_payment_blueprints (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  blueprint_key VARCHAR(160) NOT NULL DEFAULT 'operative_finance_payment_gate',
  requirement_key VARCHAR(160) NOT NULL,
  requirement_label VARCHAR(255) NOT NULL,
  requirement_group VARCHAR(60) NOT NULL DEFAULT 'payments',
  domain_key VARCHAR(80) NOT NULL DEFAULT 'payments',
  stage_key VARCHAR(120) NOT NULL DEFAULT 'payments',
  severity VARCHAR(40) NOT NULL DEFAULT 'blocker',
  required_flag TINYINT(1) NOT NULL DEFAULT 1,
  hard_block_go TINYINT(1) NOT NULL DEFAULT 1,
  responsible_role VARCHAR(80) NOT NULL DEFAULT 'reception',
  action_key VARCHAR(160) NOT NULL DEFAULT 'resolve_enterprise_finance_payment_gate',
  source_tables_json JSON NULL,
  match_tokens_json JSON NULL,
  generic_accept_allowed TINYINT(1) NOT NULL DEFAULT 0,
  sort_order INT NOT NULL DEFAULT 100,
  active TINYINT(1) NOT NULL DEFAULT 1,
  description TEXT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NULL,
  PRIMARY KEY(id),
  UNIQUE KEY uq_enterprise_finance_blueprint (blueprint_key, requirement_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_case_finance_payment_requirements (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  case_spine_id BIGINT UNSIGNED NOT NULL,
  operative_case_id BIGINT UNSIGNED NULL,
  patient_id BIGINT UNSIGNED NULL,
  requirement_key VARCHAR(160) NOT NULL,
  requirement_label VARCHAR(255) NOT NULL,
  requirement_group VARCHAR(60) NOT NULL DEFAULT 'payments',
  readiness_status VARCHAR(40) NOT NULL DEFAULT 'HOLD',
  status VARCHAR(60) NOT NULL DEFAULT 'missing',
  amount_due_cents BIGINT NULL,
  amount_paid_cents BIGINT NULL,
  balance_cents BIGINT NULL,
  currency CHAR(3) NOT NULL DEFAULT 'PLN',
  source_table VARCHAR(120) NULL,
  source_id BIGINT UNSIGNED NULL,
  context_json JSON NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NULL,
  PRIMARY KEY(id),
  UNIQUE KEY uq_enterprise_case_finance_req (case_spine_id, requirement_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_finance_payment_events (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  case_spine_id BIGINT UNSIGNED NOT NULL,
  requirement_key VARCHAR(160) NULL,
  event_type VARCHAR(80) NOT NULL,
  from_status VARCHAR(40) NULL,
  to_status VARCHAR(40) NULL,
  source_table VARCHAR(120) NULL,
  source_id BIGINT UNSIGNED NULL,
  details_json JSON NULL,
  created_by_user_id BIGINT UNSIGNED NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS enterprise_finance_payment_runs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  run_uuid VARCHAR(80) NOT NULL,
  run_type VARCHAR(80) NOT NULL DEFAULT 'manual_sync',
  scanned_cases INT UNSIGNED NOT NULL DEFAULT 0,
  evaluated_requirements INT UNSIGNED NOT NULL DEFAULT 0,
  go_cases INT UNSIGNED NOT NULL DEFAULT 0,
  hold_cases INT UNSIGNED NOT NULL DEFAULT 0,
  no_go_cases INT UNSIGNED NOT NULL DEFAULT 0,
  details_json JSON NULL,
  created_by_user_id BIGINT UNSIGNED NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY(id),
  UNIQUE KEY uq_enterprise_finance_run (run_uuid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
