<?php

namespace App\Service;

use Symfony\Contracts\HttpClient\HttpClientInterface;
use Doctrine\ORM\EntityManagerInterface; 
class Motocultrice
{
    private $overpassApiUrl = 'https://overpass-api.de/api/interpreter';
    private $osmApiUrl = 'https://www.openstreetmap.org/api/0.6';

    public $overpass_base_places = '
    (
  nw["amenity"~"^(library|townhall|restaurant|fast_food|cafe|fuel|pharmacy|bank|bar|hospital|post_office|clinic|pub|car_wash|ice_cream|driving_school|cinema|car_rental|nightclub|bureau_de_change|studio|internet_cafe|money_transfer|casino|vehicle_inspection|frozen_food|boat_rental|coworking_space|workshop|personal_service|camping|dancing_school|training|ski_school|ski_rental|dive_centre|driver_training|nursing_home|funeral_hall|doctors|dentist|theatre|kindergarten|language_school|stripclub|veterinary|convenience|supermarket|clothes|hairdresser|car_repair|bakery|beauty|car|hardware|mobile_phone|butcher|furniture|car_parts|alcohol|florist|scooter|variety_store|electronics|shoes|optician|jewelry|mall|gift|doityourself|greengrocer|books|bicycle|chemist|department_store|laundry|travel_agency|stationery|pet|sports|confectionery|tyres|cosmetics|computer|tailor|tobacco|storage_rental|dry_cleaning|trade|copyshop|motorcycle|funeral_directors|beverages|newsagent|garden_centre|massage|pastry|interior_decoration|general|deli|toys|houseware|wine|seafood|pawnbroker|tattoo|paint|wholesale|photo|second_hand|bed|kitchen|outdoor|fabric|antiques|coffee|gas|e-cigarette|perfumery|craft|hearing_aids|money_lender|appliance|electrical|tea|motorcycle_repair|boutique|baby_goods|bag|musical_instrument|dairy|pet_grooming|music|carpet|rental|fashion_accessories|cheese|chocolate|medical_supply|leather|sewing|cannabis|locksmith|games|video_games|hifi|window_blind|caravan|tool_hire|household_linen|bathroom_furnishing|shoe_repair|watches|nutrition_supplements|fishing|erotic|frame|grocery|boat|repair|weapons|gold_buyer|lighting|pottery|security|groundskeeping|herbalist|curtain|health_food|flooring|printer_ink|anime|camera|scuba_diving|candles|printing|garden_furniture|food|estate_agent|insurance|it|accountant|employment_agency|tax_advisor|financial|advertising_agency|logistics|newspaper|financial_advisor|consulting|travel_agent|coworking|moving_company|lawyer|architect|construction_company|credit_broker|graphic_design|property_management|cleaning)$"](area.searchArea);
  nw["shop"]["shop"!~"vacant"](area.searchArea);
  nw["tourism"~"^(hotel|hostel|motel|wilderness_hut|yes|chalet|gallery|guest_house|museum|zoo|theme_park|aquarium|alpine_hut|apartment)$"](area.searchArea);
  nw["healthcare"](area.searchArea);
  nw["information"="office"](area.searchArea);
  nw["office"](area.searchArea);
);

    ';
    // ne pas lister les tags qui utilisent des morceaux particuliers de formulaire pour éviter que les gens aient besoin de connaître le tag OSM
    public $excluded_tags_to_render = [
        'name',
        'wheelchair',
        'harassment_prevention',
        'image',
        'panoramax',
    ];
    // les tags OSM que l'on estime nécessaires pour un commerce
    public $base_tags = [
        'name',
        'opening_hours',
        'contact:email',
        'contact:phone',
        'contact:housenumber',
        'contact:street',
        'contact:website',
        'contact:mastodon',
        'image',
        'note'
    ];

    // quand un commerce a fermé, on peut supprimer ces tags
    public $obsolete_tags = [
        "phone", "website", "email", "description", "brand", "opening_hours", 
                "check_date:opening_hours", "internet_access", 
                "indoor_seating", "takeaway", "female", "male", "unisex", 
                "ref:FR:NAF", "ref:FR:FINESS", "ref:FR:SIRET", "ref:FR:SIREN", "ref:vatin", 
				"healthcare", "dispensing", "lawyer", "vending", "vending_machine", 
                "self_service", "second_hand", "branch", "delivery", "start_date", 
                "beauty", "facebook", "tobacco", "bulk_purchase", 
                "drive_through", "pastry", "stroller", "fax", "trade", "network", 
                "mobile", "sport", "produce", "lottery", "supermarket", "information", 
                "tourism", "government", "brewery"
    ];
    public $tags_to_remove = [
        "diet:", "contact:", "name:", "payment:", "delivery:", "type:FR:", "ref:FR:SDIS:",
                "brand:", "fuel:", "service:", "description:", "operator:", "tickets:", "healthcare:"
    ];
    public $tags_to_convert = [
        "shop" => "was:shop", 
"information" => "was:information",
"office" => "was:office", 
"amenity" => "was:amenity",
"craft" => "was:craft",
"operator" => "was:operator",
"clothes" => "was:clothes", 
"cuisine" => "was:cuisine",
"official_name" => "was:official_name",
"short_name" => "was:short_name",
"alt_name" => "was:alt_name"

];


public function find_siret($tags) {
    if(isset($tags['ref:FR:SIRET']) && $tags['ref:FR:SIRET'] != '') {
        return $tags['ref:FR:SIRET'];
    }
    return null;
}

    public function export($zone) {
        $query = $this->get_export_query($zone);
        try {
            $response = $this->client->request('POST', 'https://overpass-api.de/api/interpreter', [
                'body' => ['data' => $query],
                'timeout' => 120 // Augmenter le timeout pour les exports CSV
            ]);

            if ($response->getStatusCode() !== 200) {
                throw new \Exception('L\'API Overpass a retourné un code de statut non-200 : ' . $response->getStatusCode());
            }

            return $response->getContent();
        } catch (\Exception $e) {
            return "Erreur lors de la requête Overpass : " . $e->getMessage();
        }
    }

    public function get_export_query($zone) {
        return <<<QUERY
[out:csv(::id,::type,::lat,::lon,::timestamp,::version,::user,::uid,::changeset,name,amenity,shop,office,healthcare,"contact:email",email,"contact:phone",phone,"contact:website",website,image,url,wikidata,opening_hours,"contact:housenumber","addr:housenumber","contact:street","addr:street",note,fixme,harassment_prevention,cuisine,brand,tourism,source,zip_code,"ref:FR:SIRET")];
area["ref:INSEE"="{$zone}"]->.searchArea;
{$this->overpass_base_places}
out meta;
QUERY;
    }

    public function get_query_places($zone) {
        return '[out:json][timeout:25];
area["ref:INSEE"="'.$zone.'"]->.searchArea;
'.$this->overpass_base_places.'
out meta;';
    }

    private $more_tags = ['image', 'ref:FR:SIRET'];
    public function __construct(
        private HttpClientInterface $client,
        private EntityManagerInterface $entityManager
    ) {
    }

    /**
     * fait le lien entre les tags du formulaire et les tags OSM pour les champs personnalisés
     *
     * @param [type] $request_post
     * @return void
     */
    public function map_post_values($request_post) {
        $has_ask_angela = false; 
        $has_opening_hours = false; 

        $modified_request_post = [];

        foreach ($request_post as $key => $value) {
            if (strpos($key, 'custom__ask_angela') === 0 ) {
                if($value == 'ask_angela'){
                    $has_ask_angela = true;
                } 
            }
            if (strpos($key, 'custom__opening_hours') === 0 && $value != '') {
                $has_opening_hours = true;
            }
            // Ajout gestion livraison/vente à emporter (takeaway)
            if (strpos($key, 'custom__takeaway') === 0) {
                if (in_array($value, ['yes', 'only', 'no'])) {
                    $modified_request_post['commerce_tag_value__takeaway'] = $value;
                }
                // Si vide ou autre, on ne met pas le champ
            }
            // Gestion spéciale pour le champ cuisine (multiple)
            if (strpos($key, 'commerce_tag_value__cuisine') === 0) {
                $newCuisines = $value;
                if (!is_array($newCuisines)) {
                    $newCuisines = array_map('trim', explode(';', $newCuisines));
                }
                // Récupérer la valeur existante si présente
                $existing = isset($request_post['cuisine_existing']) ? $request_post['cuisine_existing'] : '';
                $existingCuisines = $existing ? array_map('trim', explode(';', $existing)) : [];
                // Fusionner en gardant l'ordre de l'existant puis des nouveaux non déjà présents
                $allCuisines = $existingCuisines;
                foreach ($newCuisines as $c) {
                    if ($c !== '' && !in_array($c, $allCuisines)) {
                        $allCuisines[] = $c;
                    }
                }
                // Nettoyer les vides et assembler
                $allCuisines = array_filter($allCuisines, fn($v) => $v !== '');
                $modified_request_post['commerce_tag_value__cuisine'] = implode(';', $allCuisines);
            }
            $modified_request_post[$key] = $value;
        }

        if($has_ask_angela) {
            $modified_request_post['commerce_tag_value__harassment_prevention'] = 'ask_angela';
        } 

        if($has_opening_hours) {
            $modified_request_post['commerce_tag_value__opening_hours'] = $request_post['commerce_tag_value__opening_hours'];
        }

        return $modified_request_post;
    }


    public function labourer(string $zone): array
    {
        $query = $this->get_query_places($zone);
        $url = 'https://overpass-api.de/api/interpreter?data=' . urlencode($query);

        try {
            $response = $this->client->request('GET', $url, [
                'timeout' => 90, // Augmenter le timeout pour les zones très denses
            ]);

            if ($response->getStatusCode() !== 200) {
                throw new \Exception('L\'API Overpass a retourné un code de statut non-200 : ' . $response->getStatusCode());
            }

            $data = json_decode($response->getContent(), true);

            if (json_last_error() !== JSON_ERROR_NONE) {
                // Tenter de récupérer le corps de la réponse brute en cas d'erreur JSON pour le débogage
                $rawResponse = $response->getContent(false); // ne pas lancer d'exception si la réponse n'est pas décodable
                throw new \Exception('Réponse JSON invalide depuis l\'API Overpass. Erreur: ' . json_last_error_msg() . '. Réponse brute : ' . substr($rawResponse, 0, 500));
            }
            
            $places = [];
            if (isset($data['elements'])) {
                if (count($data['elements']) === 0) {
                    // Ce n'est pas une erreur, juste aucun lieu trouvé. On retourne un tableau vide.
                    return [];
                }

                foreach ($data['elements'] as $element) {
                    if (isset($element['tags'])) { 
                        
                        $places[] = [
                            'id' => $element['id'],
                            'type' => $element['type'],
                            'name' => $element['tags']['name'] ?? '',
                            'lat' => $element['lat'] ?? null,
                            'lon' => $element['lon'] ?? null,
                            'tags' => $element['tags'],
                            // Métadonnées OSM
                            'timestamp' => $element['timestamp'] ?? null,
                            'version' => $element['version'] ?? null,
                            'user' => $element['user'] ?? null,
                            'uid' => $element['uid'] ?? null,
                            'changeset' => $element['changeset'] ?? null,
                            'modified' => $element['timestamp'] ?? null
                        ];
                    }
                }
            }

            // Libérer la mémoire
            unset($data);
            gc_collect_cycles();

            return $places;

        } catch (\Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface $e) {
            // Gérer spécifiquement les erreurs de transport (timeout, problème de connexion)
            throw new \Exception('Erreur de communication avec l\'API Overpass (le service est peut-être indisponible ou la requête a expiré) : ' . $e->getMessage(), 0, $e);
        } catch (\Exception $e) {
            // Rattraper et relancer les autres exceptions (y compris celles que nous avons définies)
            throw $e;
        }
    }

    public function find_street($tags) {
        if(isset($tags['addr:street']) && $tags['addr:street'] != '') {
            return $tags['addr:street'];
        }
        if(isset($tags['contact:street']) && $tags['contact:street'] != '') {
            return $tags['contact:street'];
        }
        return null;
    }

    public function find_housenumber($tags) {
        if(isset($tags['addr:housenumber']) && $tags['addr:housenumber'] != '') {
            return $tags['addr:housenumber'];
        }   
        if(isset($tags['contact:housenumber']) && $tags['contact:housenumber'] != '') {
            return $tags['contact:housenumber'];
        }
        return null;
    }

    public function find_tag($tags, $tag) {
        if(isset($tags[$tag]) && $tags[$tag] != '') {
            return $tags[$tag];
        }
        return null;
    }

    public function get_city_osm_from_zip_code($zip_code) {
        // Détection spéciale pour Paris, Lyon, Marseille
        if (preg_match('/^75(0[1-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|6[0-9]|7[0-9]|8[0-9]|9[0-9])$/', $zip_code)) {
            $arr = intval(substr($zip_code, 2, 3));
            return 'Paris ' . $arr . 'e arr.';
        }
        if (preg_match('/^69(0[1-9]|1[0-9]|2[0-9])$/', $zip_code)) {
            $arr = intval(substr($zip_code, 2, 3));
            return 'Lyon ' . $arr . 'e arr.';
        }
        if (preg_match('/^13(0[1-9]|1[0-6])$/', $zip_code)) {
            $arr = intval(substr($zip_code, 2, 3));
            return 'Marseille ' . $arr . 'e arr.';
        }
        // Requête Overpass pour obtenir la zone administrative de niveau 8 avec un nom
        $query = "[out:json][timeout:25];\n        area[\"ref:INSEE\"=\"{$zip_code}\"]->.searchArea;\n        (\n            relation[\"admin_level\"=\"8\"][\"name\"][\"type\"=\"boundary\"][\"boundary\"=\"administrative\"](area.searchArea);\n        );\n        out body;\n        >;\n        out skel qt;";
        $response = $this->client->request('POST', $this->overpassApiUrl, [
            'body' => ['data' => $query]
        ]);
        $data = json_decode($response->getContent(), true);
        if (isset($data['elements']) && !empty($data['elements'])) {
            $city = $data['elements'][0]['tags']['name'];
            return $city;
        }
        return null;
    }

    public function get_osm_object_data($osm_kind = 'node', $osm_object_id = 12855459190)
    { 
        $object_id = "https://www.openstreetmap.org/api/0.6/".$osm_kind."/".$osm_object_id;
    

        try {
            $response = $this->client->request('GET', $object_id);
            $xml = simplexml_load_string($response->getContent());
            $json = json_encode($xml);
            $osm_object_data = json_decode($json, true);
        } catch (\Exception $e) {
            throw new \Exception("Impossible de récupérer les données OSM : " . $e->getMessage());
        }

        // convertir les tags en clés et valeurs, remplir avec les tags de base
        $osm_object_data['tags_converted'] = $this->base_tags;
        // Initialiser le tableau des tags convertis
        if (isset($osm_object_data['node'])) {
            $osm_object_data['node']['tags_converted'] = [];
        } elseif (isset($osm_object_data['way'])) {
            $osm_object_data['way']['tags_converted'] = [];
        }
        if(isset($osm_object_data['node'])){ 
            // Vérifier si le nœud a des tags
            if (!isset($osm_object_data['node']['tag'])) {
                $osm_object_data['node']['tags_converted'] = [];
                return $osm_object_data['node'];
            }

            // Si un seul tag, le convertir en tableau
            if (isset($osm_object_data['node']['tag']['@attributes'])) {
                $osm_object_data['node']['tag'] = [$osm_object_data['node']['tag']];
            }
            foreach ($osm_object_data['node']['tag'] as $tag) {
                $osm_object_data['node']['tags_converted'][$tag['@attributes']['k']] = $tag['@attributes']['v'];
            }
            $osm_object_data['node']['tags_converted'] = $this->migrate_tags($osm_object_data['node']['tags_converted']);
            return $osm_object_data['node'];
        }
        if(isset($osm_object_data['way'])){
            // Vérifier si le way a des tags
            if (!isset($osm_object_data['way']['tag'])) {
                $osm_object_data['way']['tags_converted'] = [];
                return $osm_object_data['way'];
            }

            // Si un seul tag, le convertir en tableau
            if (isset($osm_object_data['way']['tag']['@attributes'])) {
                $osm_object_data['way']['tag'] = [$osm_object_data['way']['tag']];
            }
            foreach ($osm_object_data['way']['tag'] as $attribute) {
                $osm_object_data['way']['tags_converted'][$attribute['@attributes']['k']] = $attribute['@attributes']['v'];
            }
            $osm_object_data['way']['tags_converted'] = $this->migrate_tags($osm_object_data['way']['tags_converted']);
            return $osm_object_data['way'];
        }

        return $osm_object_data;
    }
    

    public function find_main_tag($tags) {
        if(isset($tags['amenity']) && $tags['amenity'] != '') {
            return $tags['amenity'];
        }
        if(isset($tags['shop']) && $tags['shop'] != '') {
            return $tags['shop'];
        }
        if(isset($tags['tourism']) && $tags['tourism'] != '') {
            return $tags['tourism'];
        }
        if(isset($tags['healthcare']) && $tags['healthcare'] != '') {
            return $tags['healthcare'];
        }
        if(isset($tags['office']) && $tags['office'] != '') {
            return $tags['office'];
        }
        return null;
    }

    /**
     * migrer seulement si la destination n'est pas remplie
     */
    public function migrate_tags($osm_object_data) {

        // migrer email vers contact:email
        if(isset($osm_object_data['email']) && !isset($osm_object_data['contact:email'])){
            $osm_object_data['contact:email'] = $osm_object_data['email'];
            unset($osm_object_data['email']);
        }
        // migrer phone vers contact:phone
        if(isset($osm_object_data['phone']) && !isset($osm_object_data['contact:phone'])){
            $osm_object_data['contact:phone'] = $osm_object_data['phone'];
            unset($osm_object_data['phone']);
        }
        // migrer website vers contact:website
        if(isset($osm_object_data['website']) && !isset($osm_object_data['contact:website'])){
            $osm_object_data['contact:website'] = $osm_object_data['website'];
            unset($osm_object_data['website']);
        }
        // migrer addr:housenumber vers contact:housenumber
        if(isset($osm_object_data['addr:housenumber']) && !isset($osm_object_data['contact:housenumber'])){
            $osm_object_data['contact:housenumber'] = $osm_object_data['addr:housenumber'];
            unset($osm_object_data['addr:housenumber']);
        }
        // migrer addr:street vers contact:street
        if(isset($osm_object_data['addr:street']) && !isset($osm_object_data['contact:street'])){
            $osm_object_data['contact:street'] = $osm_object_data['addr:street'];
            unset($osm_object_data['addr:street']);
        }
        
        return $osm_object_data;
    }
        
    

    public function uuid_create() {
    return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        // 32 bits for "time_low"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

        // 16 bits for "time_mid"
        mt_rand( 0, 0xffff ),

        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand( 0, 0x0fff ) | 0x4000,

        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand( 0, 0x3fff ) | 0x8000,

        // 48 bits for "node"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
    );
}

    public function formatOsmDataForSubmit(array $data): array
    {
        // Garder uniquement les tags essentiels
        $essentialTags = [
            'name',
            'opening_hours',
            // 'phone', 
            'contact:email',
            'contact:phone',
            'website',
            'contact:website',
            'wheelchair',
            'addr:housenumber',
            'addr:street',
            'addr:city',
            'addr:postcode',
            'amenity',
            'shop',
            'tourism',
            'source',
            'ref:FR:SIRET'
        ];

        $formattedData = [
            'node' => [
                '@attributes' => [
                    'id' => $data['@attributes']['id'],
                    'version' => $data['@attributes']['version'],
                    'changeset' => $data['@attributes']['changeset'],
                    'lat' => $data['@attributes']['lat'],
                    'lon' => $data['@attributes']['lon']
                ],
                'tag' => []
            ]
        ];

        // Filtrer et ajouter uniquement les tags essentiels
        if (isset($data['tag'])) {
            foreach ($data['tag'] as $tag) {
                if (in_array($tag['@attributes']['k'], $essentialTags)) {
                    $formattedData['node']['tag'][] = $tag;
                }
            }
        }

        return $formattedData;
    }

    private function arrayToXml(array $data): string
    {
        $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><osm></osm>');
        
        if (isset($data['node'])) {
            $node = $xml->addChild('node');
            foreach ($data['node']['@attributes'] as $key => $value) {
                $node->addAttribute($key, $value);
            }
            
            if (isset($data['node']['tag'])) {
                foreach ($data['node']['tag'] as $tag) {
                    $tagElement = $node->addChild('tag');
                    $tagElement->addAttribute('k', $tag['@attributes']['k']);
                    $tagElement->addAttribute('v', $tag['@attributes']['v']);
                }
            }
        }
        
        return $xml->asXML();
    }

    public function submitOsmData(array $data): void
    {
        $formattedData = $this->formatOsmDataForSubmit($data);
        $xmlData = $this->arrayToXml($formattedData);
        
        try {
            $response = $this->client->request('PUT', 
                "{$this->osmApiUrl}/node/{$data['@attributes']['id']}", 
                [
                    'body' => $xmlData,
                    'headers' => [
                        'Content-Type' => 'application/xml; charset=utf-8'
                    ]
                ]
            );

            if ($response->getStatusCode() !== 200) {
                throw new \Exception("Erreur lors de la soumission des données : " . $response->getContent());
            }
        } catch (\Exception $e) {
            throw new \Exception("Erreur lors de la communication avec l'API OSM : " . $e->getMessage());
        }
    }

    public function calculateStats(array $places): array
    {
        $counters = [
            'avec_horaires' => 0,
            'avec_adresse' => 0,
            'avec_site' => 0,
            'avec_accessibilite' => 0,
            'avec_note' => 0
        ];

        foreach ($places as $place) {
            if ($place->hasOpeningHours()) {
                $counters['avec_horaires']++;
            }
            if ($place->hasAddress()) {
                $counters['avec_adresse']++;
            }
            if ($place->hasWebsite()) {
                $counters['avec_site']++;
            }
            if ($place->hasWheelchair()) {
                $counters['avec_accessibilite']++;
            }
            if ($place->hasNote()) {
                $counters['avec_note']++;
            }
        }

        $totalPlaces = count($places);
        $completionPercent = 0;
        
        if ($totalPlaces > 0) {
            $totalCriteria = 5; // nombre total de critères
            $totalCompleted = array_sum($counters);
            $completionPercent = round(($totalCompleted / ($totalPlaces * $totalCriteria)) * 100);
        }

        return [
            'places_count' => $totalPlaces,
            'completion_percent' => $completionPercent,
            'counters' => $counters
        ];
    }

    /**
     * Génère la requête Overpass pour tous les objets de suivi (thématiques étendues)
     */
    public function get_followup_query($zone) {
        return <<<QUERY
[out:json][timeout:60];
area["ref:INSEE"="$zone"]->.searchArea;
(
  nwr["emergency"="fire_hydrant"](area.searchArea);
  nwr["amenity"="charging_station"](area.searchArea);
  nwr["amenity"="toilets"](area.searchArea);
  nwr["highway"="bus_stop"](area.searchArea);
  nwr["emergency"="defibrillator"](area.searchArea);
  nwr["man_made"="surveillance"](area.searchArea);
  nwr["amenity"="recycling"](area.searchArea);
  nwr["power"="substation"](area.searchArea);
  nwr["healthcare"](area.searchArea);
  nwr["amenity"="doctors"](area.searchArea);
  nwr["amenity"="pharmacy"](area.searchArea);
  nwr["amenity"="hospital"](area.searchArea);
  nwr["amenity"="clinic"](area.searchArea);
  nwr["amenity"="social_facility"](area.searchArea);
  nwr["healthcare"="laboratory"](area.searchArea);
  nwr["amenity"="school"](area.searchArea);
  nwr["amenity"="police"](area.searchArea);
  nwr["amenity"="bicycle_parking"](area.searchArea);
  nwr["advertising"="board"]["message"="political"](area.searchArea);
  way["building"](area.searchArea);
  nwr["email"](area.searchArea);
  nwr["contact:email"](area.searchArea);
  nwr["amenity"="bench"](area.searchArea);
  nwr["amenity"="waste_basket"](area.searchArea);
  nwr["highway"="street_lamp"](area.searchArea);
  nwr["amenity"="drinking_water"](area.searchArea);
  nwr["natural"="tree"](area.searchArea);
  nw["power"="pole"](area.searchArea);
);
(._;>;);
out meta;
>;
QUERY;
    }

    /**
     * Récupère les objets de suivi pour une ville (bornes incendie et bornes de recharge)
     */
    public function followUpCity($zone) {
        $query = $this->get_followup_query($zone);
        try {
            $response = $this->client->request('POST', 'https://overpass-api.de/api/interpreter', [
                'body' => ['data' => $query],
                'timeout' => 60
            ]);
            if ($response->getStatusCode() !== 200) {
                throw new \Exception('L\'API Overpass a retourné un code de statut non-200 : ' . $response->getStatusCode());
            }
            $data = json_decode($response->getContent(), true);
            return $data['elements'] ?? [];
        } catch (\Exception $e) {
            return [];
        }
    }

    /**
     * Calcule la progression sur 7j, 30j, 6 mois, 1 an pour une série de points [{date, value}]
     * @param array $data
     * @param \DateTime|null $now
     * @return array
     */
    public function calculateProgressions(array $data, ?\DateTime $now = null): array
    {
        $periods = [
            '7j' => '-7 days',
            '30j' => '-30 days',
            '6m' => '-6 months',
            '1a' => '-1 year',
        ];
        $progressions = [];
        if ($now === null) $now = new \DateTime();
        $calculateDelta = function(array $data, \DateTime $refDate) {
            if (empty($data)) return null;
            $last = end($data)['value'];
            $exactRef = null;
            foreach (array_reverse($data) as $point) {
                $pointDate = new \DateTime($point['date']);
                if ($pointDate <= $refDate) {
                    $exactRef = $point['value'];
                    break;
                }
            }
            if ($exactRef !== null) return $last - $exactRef;
            $beforeRef = null; $afterRef = null; $beforeDate = null; $afterDate = null;
            foreach (array_reverse($data) as $point) {
                $pointDate = new \DateTime($point['date']);
                if ($pointDate < $refDate) { $beforeRef = $point['value']; $beforeDate = $pointDate; break; }
            }
            foreach ($data as $point) {
                $pointDate = new \DateTime($point['date']);
                if ($pointDate > $refDate) { $afterRef = $point['value']; $afterDate = $pointDate; break; }
            }
            if ($beforeRef !== null && $afterRef !== null && $beforeDate && $afterDate) {
                $timeDiff = $afterDate->getTimestamp() - $beforeDate->getTimestamp();
                $refTimeDiff = $refDate->getTimestamp() - $beforeDate->getTimestamp();
                $ratio = $refTimeDiff / $timeDiff;
                $interpolatedRef = $beforeRef + ($afterRef - $beforeRef) * $ratio;
                return $last - $interpolatedRef;
            }
            if ($beforeRef !== null) return $last - $beforeRef;
            if ($afterRef !== null) return $last - $afterRef;
            return null;
        };
        foreach ($periods as $label => $mod) {
            $refDate = (clone $now)->modify($mod);
            $progressions[$label] = $calculateDelta($data, $refDate);
        }
        return $progressions;
    }
} 