-- Phase 4 (2026-04-26) — Système Soudoyer POI : police, mairie, tribunal.
--
-- Le joueur PAIE le POI gouvernemental (vs racket = il SE FAIT PAYER par
-- les commerces). En échange : réduction de chaleur, items judiciaires, et
-- pour les tribunaux : libération de tous les gangsters incarcérés du gang.
--
-- Tarifs crescendo, ponctuel (1 paiement = 1 effet) ou récurrent (auto chaque
-- jour/mois selon poi_kind). Si récurrent + fonds insuffisants → cancel auto.
--
-- Cap : 1 bribe par joueur × par POI × par jour (toutes tiers confondus pour
-- police, par tier pour mairie/tribunal — mais la consigne user 2026-04-23
-- aligne tout sur "1×/joueur/POI/jour" pour simplicité).
--
-- Idempotente.

CREATE TABLE IF NOT EXISTS poi_bribes (
  id                       uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
  player_id                uuid NOT NULL,
  poi_id                   uuid NOT NULL,
  poi_kind                 varchar(20) NOT NULL,                  -- 'police' | 'townhall' | 'courthouse'
  tier                     smallint NOT NULL,                     -- 1..3
  amount_per_cycle         integer NOT NULL,
  currency                 varchar(20) NOT NULL DEFAULT 'infinite_shadow',
  heat_delta_per_cycle     numeric(6,2) NOT NULL,                 -- ex: -1.0, -20.0
  cycle_kind               varchar(10) NOT NULL,                  -- 'daily' | 'monthly' | 'one_shot'
  is_recurring             boolean NOT NULL DEFAULT false,
  status                   varchar(20) NOT NULL DEFAULT 'active', -- 'active' | 'cancelled' | 'insufficient_funds' | 'completed'
  next_payment_at          timestamp NOT NULL,
  last_paid_at             timestamp NOT NULL DEFAULT now(),
  cancel_reason            varchar(30),
  cancelled_at             timestamp,
  -- Items granted (Alibi/Jurisprudence pour tribunal tier 2/3) — log tracking
  granted_items            jsonb NOT NULL DEFAULT '[]'::jsonb,
  -- Bonus tribunal tier 3 : libère tous les gangsters du gang direct du joueur
  released_gang_members    jsonb NOT NULL DEFAULT '[]'::jsonb,
  created_at               timestamp NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS idx_poi_bribes_active
  ON poi_bribes (player_id, poi_kind, status) WHERE status = 'active';

CREATE INDEX IF NOT EXISTS idx_poi_bribes_due
  ON poi_bribes (next_payment_at) WHERE status = 'active' AND is_recurring = true;

-- Cap "1×/joueur/POI/jour" enforced at app layer (PoiBribeService.bribe check)
-- via la query : SELECT 1 FROM poi_bribes WHERE player_id=... AND poi_id=...
--   AND last_paid_at >= date_trunc('day', now()) AND status='active'.
-- Pas d'index UNIQUE car les rows complétées (status='completed') doivent
-- pouvoir cohabiter avec une nouvelle active le lendemain.

COMMENT ON TABLE poi_bribes IS
  'Phase 4 — Soudoyer POI gouvernemental (police/mairie/tribunal). Le joueur paie pour réduire sa chaleur et obtenir items judiciaires (Alibi, Jurisprudence). Récurrence quotidienne/mensuelle. Cap 1×/joueur/POI/jour.';
COMMENT ON COLUMN poi_bribes.poi_kind IS '''police'' | ''townhall'' | ''courthouse''';
COMMENT ON COLUMN poi_bribes.granted_items IS 'Liste d''items judiciaires accordés au paiement (jsonb [{item_subtype, quantity}])';
