-- 016 — Renforts temporaires (24h) pour le service gang "Besoin de renfort"
-- Génère 3 gangsters par palier d'1 km d'influence autour du Point de Territoire.
-- Max 99. Expirent après 24h.

CREATE TABLE IF NOT EXISTS temporary_reinforcements (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  owner_type VARCHAR(10) NOT NULL CHECK (owner_type IN ('player','gang','cartel')),
  owner_id UUID NOT NULL,
  -- Lien vers l'entité joueur NPC générée (pour combat / visibilité)
  gangster_player_id UUID,
  source_gang_id UUID, -- gang NPC (Marcello/RepubliVain/MaraGracia) qui envoie le renfort
  lat DECIMAL(10,7) NOT NULL,
  lng DECIMAL(10,7) NOT NULL,
  hp INT NOT NULL DEFAULT 100,
  hp_max INT NOT NULL DEFAULT 100,
  weapon_id UUID, -- arme équipée (référence PlayerInventory si généré)
  armor_id UUID,
  -- Home base à rejoindre (Point de Territoire)
  home_lat DECIMAL(10,7),
  home_lng DECIMAL(10,7),
  -- Ordre courant (goto_base | engage_enemy | idle_at_base | dead)
  current_order VARCHAR(20) NOT NULL DEFAULT 'goto_base',
  target_enemy_id UUID,
  spawned_at TIMESTAMP NOT NULL DEFAULT NOW(),
  expires_at TIMESTAMP NOT NULL,
  died_at TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_reinf_owner ON temporary_reinforcements(owner_type, owner_id, died_at);
CREATE INDEX IF NOT EXISTS idx_reinf_expires_at ON temporary_reinforcements(expires_at) WHERE died_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_reinf_coords ON temporary_reinforcements(lat, lng);
