-- ============================================================
-- Migration 075 (Phase 17 — Détective + Médecin perso, 2026-05-04)
-- ============================================================
-- Ajoute 2 nouveaux rôles à l'enum specialist_role :
--   • 'detective' — Enquêteur (révèle 1 POI player-stocked / 7 jours)
--   • 'medic'     — Médecin perso (heal passif +1 PV/h + on-call)
--
-- Colonnes additionnelles sur specialist_attachments pour porter les
-- mécaniques propres à ces 2 rôles :
--   • next_report_at        : date du prochain rapport hebdo (Détective)
--   • last_report_at        : date du dernier rapport rendu (audit)
--   • passive_heal_last_at  : dernier tick +1 PV/h (Médecin perso)
--
-- Table poi_revelations : POI dévoilés au joueur par son détective,
-- avec date d'expiration (révélation temporaire 7 jours).
--
-- Idempotente : ADD VALUE IF NOT EXISTS, ADD COLUMN IF NOT EXISTS,
--              CREATE TABLE IF NOT EXISTS.
-- ============================================================

-- 1) Étendre l'enum specialist_role (TypeORM le déclare en varchar(20)
--    contraint applicativement via le type TS, pas un enum Postgres
--    natif). Donc rien à faire côté DB pour l'enum lui-même : un check
--    constraint applicatif suffit. Voir SpecialistService.SPECIALIST_ROLES.
--    Note : si ce projet bascule un jour sur un vrai enum PG, ajouter
--    un ALTER TYPE ici.

-- 2) Colonnes de cycle pour Détective + Médecin
ALTER TABLE specialist_attachments
  ADD COLUMN IF NOT EXISTS next_report_at timestamp NULL,
  ADD COLUMN IF NOT EXISTS last_report_at timestamp NULL,
  ADD COLUMN IF NOT EXISTS passive_heal_last_at timestamp NULL;

CREATE INDEX IF NOT EXISTS idx_spec_att_next_report
  ON specialist_attachments (next_report_at)
  WHERE next_report_at IS NOT NULL AND is_active = true;

CREATE INDEX IF NOT EXISTS idx_spec_att_passive_heal
  ON specialist_attachments (passive_heal_last_at)
  WHERE passive_heal_last_at IS NOT NULL AND is_active = true;

-- 3) Table poi_revelations — POI dévoilés temporairement par un detective
--    à un owner (player|gang|cartel). 1 ligne par révélation active.
--    Cleanup auto via cron quand revealed_until < NOW().
CREATE TABLE IF NOT EXISTS poi_revelations (
  id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
  owner_type varchar(10) NOT NULL,        -- 'player' | 'gang' | 'cartel'
  owner_id uuid NOT NULL,
  poi_type varchar(40) NOT NULL,          -- 'hideout' | 'warehouse' | 'business' | …
  poi_id uuid NOT NULL,
  source_attachment_id uuid NULL,         -- specialist_attachment qui a révélé (audit)
  details_json jsonb NULL,                -- contenu rapport (stock, owner, etc.)
  revealed_at timestamp NOT NULL DEFAULT NOW(),
  revealed_until timestamp NOT NULL,      -- expiration (typiquement +7j)
  CONSTRAINT poi_revelations_owner_check CHECK (owner_type IN ('player','gang','cartel'))
);

CREATE INDEX IF NOT EXISTS idx_poi_revelations_owner
  ON poi_revelations (owner_type, owner_id, revealed_until DESC);

-- Pas de predicate WHERE revealed_until > NOW() : PostgreSQL refuse les
-- fonctions non-IMMUTABLE dans les index partiels. Le full index sur
-- revealed_until suffit pour filtrer côté query (cleanup cron).
CREATE INDEX IF NOT EXISTS idx_poi_revelations_revealed_until
  ON poi_revelations (revealed_until);
