<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    exit(0);
}

require_once 'config.php';

try {
    $siteID = isset($_GET['SiteID']) ? trim($_GET['SiteID']) : '';

    if (empty($siteID)) {
        echo json_encode([
            'success' => false,
            'message' => 'Paramètre SiteID manquant'
        ]);
        exit;
    }

    // Récupérer les informations du site
    $stmt = $pdo->prepare("
        SELECT SiteID, Nom_Site, Nom_SiteAR
        FROM Sites
        WHERE SiteID = ?
    ");
    $stmt->execute([$siteID]);
    $site = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($site) {
        echo json_encode([
            'success' => true,
            'message' => 'Informations du site récupérées',
            'sites' => [$site]
        ]);
    } else {
        echo json_encode([
            'success' => false,
            'message' => 'Site non trouvé'
        ]);
    }

} catch (Exception $e) {
    error_log("Erreur get_sites: " . $e->getMessage());
    echo json_encode([
        'success' => false,
        'message' => 'Erreur serveur: ' . $e->getMessage()
    ]);
}
?>
