-- ============================================================
-- Migration 065 (Phase 8.5d, 2026-04-29) — Hôpital infrastructure complète
-- ============================================================
-- Spec user :
--   - Mode histoire hôpital (3 missions narratives + contacts urgentistes)
--   - Achat hôpital 35M ∞ → évasion auto si GIGN/police hospitalise au même
--   - Soin progressif urgentistes (+1 PV / 10 min)
--   - GPS auto à l'hôpital le plus proche lors d'hospitalisation
--   - Frais 3000 ∞ à la sortie (clamp si insuffisant, pas de dette)
--   - Carte santé (achat pharmacy 5000) = soins gratuits
--
-- Idempotente : ADD COLUMN/CONSTRAINT IF NOT EXISTS, ON CONFLICT DO NOTHING.
-- ============================================================

-- 1. Player : flag achat hôpital + référence hôpital owned
ALTER TABLE players ADD COLUMN IF NOT EXISTS owned_hospital_business_id uuid;
ALTER TABLE players ADD COLUMN IF NOT EXISTS can_buy_hospital_unlocked boolean NOT NULL DEFAULT false;

CREATE INDEX IF NOT EXISTS idx_players_owned_hospital
  ON players(owned_hospital_business_id) WHERE owned_hospital_business_id IS NOT NULL;

ALTER TABLE players DROP CONSTRAINT IF EXISTS fk_players_owned_hospital;
ALTER TABLE players ADD CONSTRAINT fk_players_owned_hospital
  FOREIGN KEY (owned_hospital_business_id) REFERENCES businesses(id) ON DELETE SET NULL;

-- 2. PlayerHealth : healing progressif urgentiste (+1 PV / 10 min)
ALTER TABLE player_health ADD COLUMN IF NOT EXISTS healing_pts_remaining int NOT NULL DEFAULT 0;
ALTER TABLE player_health ADD COLUMN IF NOT EXISTS healing_next_tick_at timestamp;

-- 3. PoliceUnit : ETA arrivée GIGN (60s spawn → first attack territoire)
ALTER TABLE police_units ADD COLUMN IF NOT EXISTS estimated_arrival_at timestamp;

-- 4. GameSettings : tarifs + cooldowns + délais
INSERT INTO game_settings (key, value, description, category, is_public) VALUES
  ('hospital.exit_fee_default', '3000',
    'Phase 8.5d — Frais hôpital ∞ Light à la sortie (clamp si insuffisant, pas de dette)',
    'health', false),
  ('pharmacy.health_card_price', '5000',
    'Phase 8.5d — Prix carte santé en pharmacie (∞ Light, illimité 1/player)',
    'health', false),
  ('gign.spawn_to_attack_delay_seconds', '60',
    'Phase 8.5d — Délai spawn GIGN → premier dommage territoire (laisse fuir/défendre)',
    'gign', false),
  ('hospital.purchase_price', '35000000',
    'Phase 8.5d — Prix achat hôpital (∞ Light) — débloque évasion auto si hospitalisé là',
    'health', false),
  ('hospital_evasion.delay_minutes', '20',
    'Phase 8.5d — Délai entre choix mission 1 (don 50% argent) et évasion effective',
    'health', false),
  ('hospital_evasion.held_days', '8',
    'Phase 8.5d — Held remaining ms (jours) si attrappé après évasion hôpital',
    'health', false),
  ('hospital_evasion.heat_after', '95',
    'Phase 8.5d — Chaleur joueur après évasion hôpital',
    'health', false),
  ('nurse_urgentist.cooldown_days', '7',
    'Phase 8.5d — Cooldown appel infirmière urgentiste (jours)',
    'health', false),
  ('doctor_urgentist.cooldown_days', '30',
    'Phase 8.5d — Cooldown appel médecin urgentiste (jours)',
    'health', false),
  ('doctor_urgentist.cost', '500',
    'Phase 8.5d — Coût appel médecin urgentiste (∞ shadow ou light)',
    'health', false),
  ('healing.minutes_per_pv', '10',
    'Phase 8.5d — Minutes pour récupérer +1 PV via soin urgentiste',
    'health', false)
ON CONFLICT (key) DO NOTHING;

-- ============================================================
-- Contrôles post-migration
-- ============================================================
-- SELECT column_name FROM information_schema.columns
--   WHERE table_name='players' AND column_name LIKE '%hospital%';
-- SELECT column_name FROM information_schema.columns
--   WHERE table_name='player_health' AND column_name LIKE 'healing%';
-- SELECT column_name FROM information_schema.columns
--   WHERE table_name='police_units' AND column_name='estimated_arrival_at';
-- SELECT key, value FROM game_settings WHERE key LIKE 'hospital%' OR key LIKE 'nurse%' OR key LIKE 'doctor%' OR key LIKE 'healing.%' OR key LIKE 'pharmacy.%' OR key LIKE 'gign.spawn%' ORDER BY key;
