-- 027 — Gardes POI + résultats de combat d'équipe (Phase 15 Commit 3, 19-04-2026)
--
-- Q-E utilisateur : combat instantané, victimes hospitalisées grisées.
-- Un joueur peut poster 0 à 4 gardes en plus du tenancier sur un POI possédé.
-- Pas de default UUID (pgcrypto absent sur prod).

-- Gardes assignés à un POI (commerce / résidence / entrepôt / hideout / cartel_territory)
CREATE TABLE IF NOT EXISTS poi_guards (
  id UUID PRIMARY KEY,
  poi_type VARCHAR(30) NOT NULL,        -- 'business','residence','warehouse','hideout','cartel_territory'
  poi_id UUID NOT NULL,
  owner_player_id UUID NOT NULL,        -- propriétaire qui a assigné
  guard_player_id UUID NOT NULL,        -- gangster en poste (Player is_npc=true OR humain)
  assigned_at TIMESTAMP NOT NULL DEFAULT NOW(),
  released_at TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_poi_guards_target
  ON poi_guards(poi_type, poi_id) WHERE released_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_poi_guards_guard
  ON poi_guards(guard_player_id) WHERE released_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_poi_guards_owner
  ON poi_guards(owner_player_id);

-- Historique résolutions de combat (visites/attaques/braquages/extorsions)
CREATE TABLE IF NOT EXISTS combat_resolutions (
  id UUID PRIMARY KEY,
  order_type VARCHAR(20) NOT NULL,      -- 'visit','buy','attack','rob','extort','guard','develop'
  scope VARCHAR(10) NOT NULL,           -- 'gang','cartel','solo'
  scope_id UUID,
  poi_type VARCHAR(30),
  poi_id UUID,
  attacker_ids UUID[] NOT NULL,         -- liste des gangsters attaquants
  defender_ids UUID[] NOT NULL,         -- liste des défenseurs
  attack_score DECIMAL(10,2) NOT NULL,
  defense_score DECIMAL(10,2) NOT NULL,
  ratio DECIMAL(6,3) NOT NULL,
  victory BOOLEAN NOT NULL,
  attacker_casualties UUID[] NOT NULL,  -- hospitalisés côté attaquant
  defender_casualties UUID[] NOT NULL,  -- hospitalisés côté défenseur
  snapshot JSONB,                        -- détails (POI state at visit time, etc.)
  occurred_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_combat_resolutions_scope
  ON combat_resolutions(scope, scope_id, occurred_at DESC);
CREATE INDEX IF NOT EXISTS idx_combat_resolutions_poi
  ON combat_resolutions(poi_type, poi_id, occurred_at DESC);

-- Visit content snapshots : affiché sur le card POI après une visite, reste
-- tel quel jusqu'à la prochaine action visible (visit/rob/attack).
CREATE TABLE IF NOT EXISTS poi_visit_reveals (
  id UUID PRIMARY KEY,
  poi_type VARCHAR(30) NOT NULL,
  poi_id UUID NOT NULL,
  visited_by_player_id UUID NOT NULL,
  visited_by_scope VARCHAR(10) NOT NULL, -- 'gang','cartel','solo'
  visited_by_scope_id UUID,
  owner_player_id UUID,                  -- propriétaire vu au moment de la visite
  is_for_sale BOOLEAN NOT NULL DEFAULT false,
  sale_price_light INT,
  content_snapshot JSONB,                -- stocks vus, gangsters gardes présents, etc.
  revealed_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_poi_visit_reveals_poi
  ON poi_visit_reveals(poi_type, poi_id, revealed_at DESC);
CREATE INDEX IF NOT EXISTS idx_poi_visit_reveals_player
  ON poi_visit_reveals(visited_by_player_id, revealed_at DESC);
