#!/bin/bash
# Pull SVG markers depuis Iconify API (gratuit, libre, agrège game-icons + tabler + lucide + mdi)
# Tous en currentColor donc coloriables CSS.
set -e

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

# Args: $1=output_filename $2..$N=candidate iconify ids (premier qui répond 200 wins)
pull() {
  local out="$1"; shift
  local target="$OUT/$out.svg"
  if [ -f "$target" ] && [ $(stat -f%z "$target" 2>/dev/null) -gt 200 ]; then
    echo "[SKIP] $out" | tee -a "$LOG"
    return
  fi

  for id in "$@"; do
    local url="https://api.iconify.design/${id}.svg"
    local body=$(curl -sSL --max-time 8 "$url")
    if [ -n "$body" ] && [ "${body:0:9}" != "Not found" ] && [[ "$body" == *"<svg"* ]]; then
      echo "$body" > "$target"
      echo "[OK] $out ← $id ($(stat -f%z "$target") o)" | tee -a "$LOG"
      return
    fi
  done
  echo "[MISS] $out — tous les candidats échoués" | tee -a "$LOG"
}

# ============================================================
# JOUEUR / TERRITOIRE
# ============================================================
pull "marker-player"            "game-icons:person"            "tabler:user-filled"          "lucide:user"
pull "marker-player-self"       "game-icons:player-base"       "game-icons:player-next"      "tabler:user-check"
pull "marker-territory"         "game-icons:flag"              "lucide:flag"                 "tabler:flag-3-filled"
pull "marker-territory-contested" "game-icons:flag-objective"  "tabler:flag-2"               "lucide:flag-triangle-right"

# ============================================================
# VÉHICULES — civils
# ============================================================
pull "marker-vehicle-coupe-sport"       "game-icons:sports-car"         "mdi:car-sports"           "tabler:car-fan"
pull "marker-vehicle-berline"           "game-icons:dial-car"           "mdi:car"                  "tabler:car"
pull "marker-vehicle-citadine"          "mdi:car-hatchback"             "game-icons:hatchback"     "tabler:car"
pull "marker-vehicle-pickup"            "mdi:car-pickup"                "game-icons:pickup-truck"  "tabler:car"
pull "marker-vehicle-camion-livraison"  "game-icons:delivery-drone"     "mdi:truck-delivery"       "tabler:truck-delivery"
pull "marker-vehicle-camion-citerne"    "mdi:tanker-truck"              "tabler:truck"             "game-icons:cargo-crate"
pull "marker-vehicle-fourgon-blinde"    "mdi:truck-cargo-container"     "tabler:truck"             "game-icons:armored-car"
pull "marker-vehicle-corbillard"        "mdi:hearse"                    "game-icons:coffin"        "tabler:car"
pull "marker-vehicle-limousine"         "mdi:car-estate"                "game-icons:limousine"     "tabler:car"
pull "marker-vehicle-moto"              "game-icons:motorcycle"         "mdi:motorbike"            "tabler:motorbike"
pull "marker-vehicle-scooter"           "mdi:moped"                     "game-icons:scooter"       "tabler:scooter"
pull "marker-vehicle-taxi"              "game-icons:taxi"               "mdi:taxi"                 "tabler:car"

# ============================================================
# VÉHICULES — police / urgence
# ============================================================
pull "marker-vehicle-police-municipale" "mdi:police-badge-outline"      "game-icons:police-car"    "tabler:police"
pull "marker-vehicle-police-nationale"  "game-icons:police-car"         "mdi:car-emergency"        "tabler:car"
pull "marker-vehicle-gendarmerie"       "game-icons:police-officer-head" "mdi:police-station"      "tabler:badge"
pull "marker-vehicle-gign-blinde"       "mdi:tank"                      "game-icons:armored-car"   "game-icons:tank"
pull "marker-vehicle-bac"               "game-icons:police-target"      "mdi:car"                  "tabler:car"
pull "marker-vehicle-stups"             "game-icons:powder-bag"         "mdi:powder-bag"           "tabler:car"
pull "marker-vehicle-ambulance"         "game-icons:ambulance"          "mdi:ambulance"            "tabler:ambulance"
pull "marker-vehicle-pompier"           "mdi:fire-truck"                "game-icons:fire-bowl"     "tabler:truck"

# ============================================================
# VÉHICULES — air & mer
# ============================================================
pull "marker-vehicle-helicoptere"       "game-icons:helicopter"         "mdi:helicopter"           "tabler:helicopter"
pull "marker-vehicle-yacht"             "mdi:sail-boat"                 "game-icons:sailboat"      "game-icons:cruiser"
pull "marker-vehicle-ferry"             "mdi:ferry"                     "game-icons:ferry"         "game-icons:ship-bow"

# ============================================================
# POI — public services / spéciaux
# ============================================================
pull "marker-poi-prison"        "game-icons:prisoner"            "mdi:jail"                  "tabler:building-fortress"
pull "marker-poi-casino"        "game-icons:card-pickup"         "game-icons:dice-six-faces-six"  "mdi:dice-multiple"
pull "marker-poi-hopital"       "game-icons:health-normal"       "mdi:hospital-building"     "tabler:building-hospital"
pull "marker-poi-clinic"        "game-icons:first-aid-kit"       "mdi:medical-bag"           "tabler:first-aid-kit"
pull "marker-poi-mairie"        "mdi:city-hall"                  "game-icons:greek-temple"   "tabler:building-community"
pull "marker-poi-tribunal"      "mdi:scale-balance"              "game-icons:scales"         "tabler:gavel"
pull "marker-poi-commissariat"  "mdi:police-station"             "game-icons:police-officer-head" "tabler:police-station"
pull "marker-poi-eglise"        "game-icons:church"              "mdi:church"                "tabler:building-church"
pull "marker-poi-mosquee"       "game-icons:mosque"              "mdi:mosque"                "fa6-solid:mosque"
pull "marker-poi-synagogue"     "mdi:synagogue"                  "game-icons:star-of-david"  "fa6-solid:synagogue"
pull "marker-poi-temple-bouddhiste" "mdi:torii-gate"             "game-icons:eastern-temple" "game-icons:dharma-wheel"
pull "marker-poi-bibliotheque"  "game-icons:book-pile"           "mdi:library"               "tabler:books"
pull "marker-poi-poste"         "mdi:mailbox-outline"            "game-icons:envelope"       "tabler:mail"
pull "marker-poi-banque"        "mdi:bank"                       "game-icons:bank"           "tabler:building-bank"
pull "marker-poi-bureau-de-change" "mdi:cash-multiple"           "game-icons:two-coins"      "tabler:cash"
pull "marker-poi-port-ferry"    "game-icons:anchor"              "mdi:anchor"                "tabler:anchor"
pull "marker-poi-parking"       "mdi:parking"                    "game-icons:car-key"        "tabler:parking"

# ============================================================
# POI — commerces (variations multiples — choisis ce que tu préfères)
# ============================================================
pull "marker-poi-restaurant"    "game-icons:meal"                "mdi:silverware-fork-knife" "tabler:tools-kitchen-2"
pull "marker-poi-restaurant-v2" "game-icons:knife-fork"          "mdi:food"                  "tabler:meat"
pull "marker-poi-bar"           "game-icons:cocktail-glass"      "mdi:glass-cocktail"        "tabler:glass"
pull "marker-poi-bar-v2"        "game-icons:beer-stein"          "mdi:beer"                  "tabler:beer"
pull "marker-poi-nightclub"     "mdi:disco"                      "game-icons:musical-notes"  "tabler:disc"
pull "marker-poi-shop"          "game-icons:shop"                "mdi:store"                 "tabler:building-store"
pull "marker-poi-shop-v2"       "mdi:storefront"                 "tabler:building-store"     "game-icons:open-treasure-chest"
pull "marker-poi-supermarche"   "game-icons:shopping-cart"       "mdi:cart"                  "tabler:shopping-cart"
pull "marker-poi-boulangerie"   "game-icons:sliced-bread"        "mdi:bread-slice"           "tabler:bread"
pull "marker-poi-tabac"         "game-icons:smoking-finger"      "mdi:smoking"               "tabler:cigarette"
pull "marker-poi-coiffeur"      "mdi:content-cut"                "game-icons:scissors"       "tabler:cut"
pull "marker-poi-caviste"       "game-icons:wine-bottle"         "mdi:bottle-wine"           "tabler:bottle"
pull "marker-poi-pharmacy"      "mdi:medical-bag"                "game-icons:medicine-pills" "tabler:medicine-syrup"
pull "marker-poi-garage"        "game-icons:car-wheel"           "mdi:wrench"                "tabler:car-garage"
pull "marker-poi-armurerie"     "game-icons:gun-rose"            "mdi:pistol"                "tabler:point"
pull "marker-poi-carrosserie"   "game-icons:auto-repair"         "mdi:car-wash"              "game-icons:hammer-nails"
pull "marker-poi-concessionnaire" "mdi:car-multiple"             "game-icons:car-key"        "tabler:car"

# ============================================================
# POI — résidentiels (variations)
# ============================================================
pull "marker-poi-house"         "game-icons:family-house"        "mdi:home"                  "tabler:home"
pull "marker-poi-house-v2"      "mdi:home-outline"               "tabler:home-2"             "lucide:house"
pull "marker-poi-apartment"     "mdi:apartment"                  "tabler:building-skyscraper" "game-icons:office-chair"
pull "marker-poi-apartment-v2"  "tabler:building"                "lucide:building-2"         "mdi:office-building"
pull "marker-poi-villa"         "game-icons:greek-temple"        "mdi:home-modern"           "tabler:building-castle"

# ============================================================
# POI — production / industrie
# ============================================================
pull "marker-poi-ferme"         "mdi:barn"                       "game-icons:wheat"          "tabler:plant-2"
pull "marker-poi-usine"         "game-icons:factory"             "mdi:factory"               "tabler:building-factory"
pull "marker-poi-mine"          "game-icons:mine-truck"          "mdi:pickaxe"               "tabler:tools"
pull "marker-poi-fonderie"      "game-icons:foundry-bucket"      "mdi:fire"                  "tabler:flame"
pull "marker-poi-laundry"       "game-icons:washing-machine"     "mdi:washing-machine"       "tabler:wash-machine"
pull "marker-poi-warehouse"     "mdi:warehouse"                  "game-icons:cardboard-box-closed" "tabler:building-warehouse"

# ============================================================
# POI — associations
# ============================================================
pull "marker-poi-assoc-caritative" "game-icons:hearts"           "mdi:hand-heart"            "tabler:hand-stop"
pull "marker-poi-assoc-ecologique" "game-icons:tree-branch"      "mdi:leaf"                  "tabler:plant"
pull "marker-poi-assoc-sociale"    "game-icons:three-friends"    "mdi:account-group"         "tabler:users"

echo "" | tee -a "$LOG"
echo "=== DONE ===" | tee -a "$LOG"
echo "Total SVG: $(ls -1 "$OUT"/*.svg 2>/dev/null | wc -l)" | tee -a "$LOG"
echo "Total size: $(du -sh "$OUT" 2>/dev/null)" | tee -a "$LOG"
echo "MISSED: $(grep -c '\[MISS\]' "$LOG" || echo 0)" | tee -a "$LOG"
