#!/bin/bash
# Mafiaz — Download tous les SVG depuis game-icons via Iconify API
# Output: /frontend/public/images/svg/mapped/{slug}.svg
# Tous les SVG sont en currentColor (coloriables CSS)
set -e

SVG=/Users/robinprevent/Desktop/MAFIADIMANCHE/frontend/public/images/svg/mapped
mkdir -p "$SVG"
LOG=/tmp/svg-mapping-download.log
> "$LOG"

# Args: $1=output_filename (sans .svg) $2=iconify_id
pull() {
  local out="$1"
  local id="$2"
  local target="$SVG/$out.svg"

  if [ -f "$target" ] && [ $(stat -f%z "$target" 2>/dev/null) -gt 200 ]; then
    echo "[SKIP] $out" | tee -a "$LOG"
    return
  fi

  local body=$(curl -sSL --max-time 15 "https://api.iconify.design/$id.svg")
  if [ -n "$body" ] && [[ "$body" == *"<svg"* ]] && [ "${body:0:9}" != "Not found" ]; then
    echo "$body" > "$target"
    echo "[OK] $out ← $id ($(stat -f%z "$target") o)" | tee -a "$LOG"
  else
    echo "[FAIL] $out ← $id (not found)" | tee -a "$LOG"
  fi
}

# ============================================================
# A. VEHICULES (forces de l'ordre + civils + spéciaux)
# ============================================================
pull "veh-police-car"            "game-icons:police-car"             # municipale/nationale/bac/stups (couleur via CSS)
pull "veh-truck"                 "game-icons:truck"                  # camion livraison/transport
pull "veh-apc"                   "game-icons:apc"                    # blindé GIGN
pull "veh-helicopter"            "game-icons:helicopter"             # hélico GIGN
pull "veh-f1-car"                "game-icons:f1-car"                 # voiture la + rapide
pull "veh-race-car"              "game-icons:race-car"               # voiture sport
pull "veh-flatbed"               "game-icons:flatbed"                # pickup
pull "veh-flatbed-covered"       "game-icons:flatbed-covered"        # camion couvert
pull "veh-ambulance"             "game-icons:ambulance"              # ambulance (réduit chaleur)
pull "veh-bus"                   "game-icons:bus"                    # bus 20 gangsters (premium)
pull "veh-city-car"              "game-icons:city-car"               # voiture de ville
pull "veh-treehouse"             "game-icons:treehouse"              # treehouse
pull "veh-car-key"               "game-icons:car-key"                # alt concessionnaire
pull "veh-steam-locomotive"      "game-icons:steam-locomotive"       # train station (futur)

# ============================================================
# B. POI BÂTIMENTS / LIEUX
# ============================================================
pull "poi-gold-mine"             "game-icons:gold-mine"              # mine
pull "poi-hospital"              "game-icons:hospital"               # hôpital
pull "poi-factory"               "game-icons:factory"                # usine
pull "poi-house"                 "game-icons:house"                  # villa
pull "poi-castle"                "game-icons:castle"                 # château
pull "poi-saloon"                "game-icons:saloon"                 # loft / penthouse
pull "poi-shop"                  "game-icons:shop"                   # commerce générique
pull "poi-saint-basil-cathedral" "game-icons:saint-basil-cathedral"  # palace
pull "poi-desert-camp"           "game-icons:desert-camp"            # squat
pull "poi-mushroom-house"        "game-icons:mushroom-house"         # point de deal
pull "poi-congress"              "game-icons:congress"               # banque
pull "poi-family-house"          "game-icons:family-house"           # immeuble
pull "poi-teacher"               "game-icons:teacher"                # école
pull "poi-window-bars"           "game-icons:window-bars"            # bâtiment prison
pull "poi-triple-gate"           "game-icons:triple-gate"            # entrepôts 2
pull "poi-home-garage"           "game-icons:home-garage"            # garage perso (achetable)
pull "poi-mechanic-garage"       "game-icons:mechanic-garage"        # mécanicien (réparation)
pull "poi-player-base"           "game-icons:player-base"            # planque attitrée
pull "poi-windmill"              "game-icons:windmill"               # moulin
pull "poi-tipi"                  "game-icons:tipi"                   # tipi (résidence)
pull "poi-wood-cabin"            "game-icons:wood-cabin"             # cabane (résidence)
pull "poi-medieval-village-01"   "game-icons:medieval-village-01"    # manoir
pull "poi-capitol"               "game-icons:capitol"                # bâtiment officiel (réserve)
pull "poi-indian-palace"         "game-icons:indian-palace"          # temple hindou
pull "poi-yin-yang"              "game-icons:yin-yang"               # temple bouddhiste (alt)
pull "poi-venus-of-willendorf"   "game-icons:venus-of-willendorf"    # warehouse
pull "poi-guarded-tower"         "game-icons:guarded-tower"          # commerce racketé

# ============================================================
# C. PERSONNAGES (joueur, gangsters, forces)
# ============================================================
pull "char-person"               "game-icons:person"                 # gangster (au repos)
pull "char-robber"               "game-icons:robber"                 # gangster en mission braquage/vol/extorsion
pull "char-beams-aura"           "game-icons:beams-aura"             # joueur (avatar territoire)
pull "char-convict"              "game-icons:convict"                # joueur incarcéré
pull "char-mug-shot"             "game-icons:mug-shot"               # joueur en garde à vue
pull "char-tribunal-jury"        "game-icons:tribunal-jury"          # joueur en cours de jugement
pull "char-evasion"              "game-icons:evasion"                # joueur évadé peine en attente
pull "char-conqueror"            "game-icons:conqueror"              # joueur vient de gagner territoire (1 min)
pull "char-meditation"           "game-icons:meditation"             # joueur médite
pull "char-backstab"             "game-icons:backstab"               # joueur ou gangster 0 PV (1 min)
pull "char-reload-gun-barrel"    "game-icons:reload-gun-barrel"      # gangster en combat
pull "char-flamethrower-soldier" "game-icons:flamethrower-soldier"   # gangster en attaque
pull "char-nurse-female"         "game-icons:nurse-female"           # infirmière urgentiste
pull "char-nurse-male"           "game-icons:nurse-male"             # médecin urgentiste
pull "char-sherlock-holmes"     "game-icons:sherlock-holmes"        # DEA
pull "char-spy"                  "game-icons:spy"                    # FBI
pull "char-police-officer-head"  "game-icons:police-officer-head"    # police pied (municipale/nationale couleur)
pull "char-robber-mask"          "game-icons:robber-mask"            # GIGN à pied
pull "char-black-flag"           "game-icons:black-flag"             # POI point territoire joueur

# ============================================================
# D. MISSIONS (catégories)
# ============================================================
pull "mission-all-seeing-eye"    "game-icons:all-seeing-eye"         # mission "France Bétonnerie"
pull "mission-anarchy"           "game-icons:anarchy"                # mission politique
pull "mission-dream-catcher"     "game-icons:dream-catcher"          # mission rédemption
pull "mission-eagle-emblem"      "game-icons:eagle-emblem"           # mission crime
pull "mission-scales"            "game-icons:scales"                 # justice (alt tribunal)
pull "mission-wanted-reward"     "game-icons:wanted-reward"          # contrat sur joueur (futur)

# ============================================================
# E. ARMES (pour POI armurerie aléatoire)
# ============================================================
pull "weapon-tec-9"              "game-icons:tec-9"
pull "weapon-lee-enfield"        "game-icons:lee-enfield"
pull "weapon-pistol-gun"         "game-icons:pistol-gun"
pull "weapon-grenade"            "game-icons:grenade"
pull "weapon-ak47"               "game-icons:ak47"

# ============================================================
# F. RESERVE / DIVERS
# ============================================================
pull "misc-dark-squad"           "game-icons:dark-squad"             # POI réserve (générique)

echo "" | tee -a "$LOG"
echo "=== TOTAL ===" | tee -a "$LOG"
echo "OK    : $(grep -c '^\[OK\]' "$LOG" || echo 0)" | tee -a "$LOG"
echo "FAIL  : $(grep -c '^\[FAIL\]' "$LOG" || echo 0)" | tee -a "$LOG"
echo "Files : $(ls -1 "$SVG"/*.svg 2>/dev/null | wc -l)" | tee -a "$LOG"
echo "Size  : $(du -sh "$SVG" 2>/dev/null | awk '{print $1}')" | tee -a "$LOG"
