-- ============================================================
-- Migration 054 (Phase 8, 2026-04-27) — player_health prison_id + cleanup POIs
-- ============================================================
-- Idempotente :
--   • ADD COLUMN IF NOT EXISTS sur 6 colonnes
--   • DELETE conditionnel des POIs prison "vides" (current_population=0)
--   • UPDATE prisons.poi_id = NULL pour les rows orphelines après DELETE
--
-- Sémantique :
--   • prison_id              = uuid (FK prisons.id) où le joueur est incarcéré
--   • prison_building        = 'B1' (Arrivants) | 'B2' (Jungle) | 'B3' (Travailleurs)
--   • prison_activity        = 'free' | 'work' | 'study'
--   • imprisoned_at          = horodatage début effectif de la peine (≠ imprisoned_until = fin)
--   • pending_imprisonment_at = horodatage start incarcération différée (saturation 115%)
--   • pending_sentence_days  = peine prévue à appliquer au unblock
-- ============================================================

ALTER TABLE player_health ADD COLUMN IF NOT EXISTS prison_id              integer REFERENCES prisons(id) ON DELETE SET NULL;
ALTER TABLE player_health ADD COLUMN IF NOT EXISTS prison_building        varchar(2);
ALTER TABLE player_health ADD COLUMN IF NOT EXISTS prison_activity        varchar(10);
ALTER TABLE player_health ADD COLUMN IF NOT EXISTS imprisoned_at          timestamp;
ALTER TABLE player_health ADD COLUMN IF NOT EXISTS pending_imprisonment_at timestamp;
ALTER TABLE player_health ADD COLUMN IF NOT EXISTS pending_sentence_days  integer;

CREATE INDEX IF NOT EXISTS idx_player_health_prison_id ON player_health(prison_id);
CREATE INDEX IF NOT EXISTS idx_player_health_pending   ON player_health(pending_imprisonment_at) WHERE pending_imprisonment_at IS NOT NULL;

-- Constraints (drop puis recreate idempotent : on accepte de gérer la
-- duplication via DROP CONSTRAINT IF EXISTS)
ALTER TABLE player_health DROP CONSTRAINT IF EXISTS chk_prison_building;
ALTER TABLE player_health ADD CONSTRAINT chk_prison_building
  CHECK (prison_building IS NULL OR prison_building IN ('B1','B2','B3'));

ALTER TABLE player_health DROP CONSTRAINT IF EXISTS chk_prison_activity;
ALTER TABLE player_health ADD CONSTRAINT chk_prison_activity
  CHECK (prison_activity IS NULL OR prison_activity IN ('free','work','study'));

-- ============================================================
-- Cleanup : retirer les POIs prison legacy avec 0 détenu
-- (5 POIs pré-Phase 8 : Fleury, La Santé, Baumettes, Fresnes, Saint-Quentin-Fallavier)
-- Ils seront recréés à la demande quand un joueur y sera assigné.
-- ============================================================
DELETE FROM poi WHERE id IN (
  SELECT poi_id FROM prisons WHERE poi_id IS NOT NULL AND current_population = 0
);
UPDATE prisons SET poi_id = NULL WHERE current_population = 0;

-- ============================================================
-- Contrôles post-migration (decommenter pour debug)
-- ============================================================
-- SELECT COUNT(*) FROM poi WHERE icon='prison';                              -- Attendu: 0
-- SELECT COUNT(*) FROM prisons WHERE poi_id IS NOT NULL;                     -- Attendu: 0
-- SELECT column_name FROM information_schema.columns
--   WHERE table_name='player_health' AND column_name LIKE 'prison_%' OR column_name LIKE 'pending_%' OR column_name='imprisoned_at';
