-- Phase 15 Iteration 6.1 (19-04-2026) — Cartel gangsters dédiés (jusqu'à 99).
-- Le cartel peut recruter ses propres NPC gangsters, avec panel identique au gang.
-- Mêmes missions, même gameplay (paye, loyauté, grade, XP, allocations), scope = cartel.
--
-- Tables créées (idempotent) :
--   cartel_gangsters       : mirror de gang_members, mais gang_id → cartel_id
--   cartel_recruit_pools   : mirror de npc_recruit_pools, mais player_id → cartel_id
--
-- Aucune dépendance à pgcrypto (UUIDs générés côté TypeORM / uuid v4).

CREATE TABLE IF NOT EXISTS cartel_gangsters (
  cartel_id             UUID NOT NULL,
  player_id             UUID NOT NULL,
  role                  VARCHAR(16) NOT NULL DEFAULT 'member',
  contribution          INT NOT NULL DEFAULT 0,
  current_lat           DECIMAL(10, 8),
  current_lon           DECIMAL(11, 8),
  heading               SMALLINT NOT NULL DEFAULT 0,
  in_transit            BOOLEAN NOT NULL DEFAULT FALSE,
  guarding_building_id  UUID,
  experience            INT NOT NULL DEFAULT 0,
  grade                 SMALLINT NOT NULL DEFAULT 1,
  specialized_position  VARCHAR(50),
  influence_radius_m    INT NOT NULL DEFAULT 300,
  assigned_vehicle_id   UUID,
  assigned_weapon       VARCHAR(100),
  assigned_racket_zone_id UUID,
  hit_points            INT NOT NULL DEFAULT 100,
  hospitalized_until    TIMESTAMP,
  loyalty               INT NOT NULL DEFAULT 80,
  last_paid_at          TIMESTAMP,
  unpaid_since          TIMESTAMP,
  daily_salary          INT NOT NULL DEFAULT 50,
  total_paid            BIGINT NOT NULL DEFAULT 0,
  last_mission_at       TIMESTAMP,
  arrest_count          INT NOT NULL DEFAULT 0,
  last_arrested_at      TIMESTAMP,
  join_date             TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT pk_cartel_gangsters PRIMARY KEY (cartel_id, player_id)
);

CREATE INDEX IF NOT EXISTS idx_cartel_gangsters_cartel ON cartel_gangsters (cartel_id);
CREATE INDEX IF NOT EXISTS idx_cartel_gangsters_player ON cartel_gangsters (player_id);
CREATE INDEX IF NOT EXISTS idx_cartel_gangsters_unpaid ON cartel_gangsters (cartel_id) WHERE unpaid_since IS NOT NULL;

CREATE TABLE IF NOT EXISTS cartel_recruit_pools (
  id                UUID PRIMARY KEY,
  cartel_id         UUID NOT NULL UNIQUE,
  size              INT NOT NULL DEFAULT 0,
  has_elite         BOOLEAN NOT NULL DEFAULT FALSE,
  slots             JSONB NOT NULL DEFAULT '[]'::jsonb,
  refreshed_at      TIMESTAMP NOT NULL,
  next_refresh_at   TIMESTAMP NOT NULL,
  created_at        TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at        TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_cartel_recruit_pools_cartel ON cartel_recruit_pools (cartel_id);
