<?php
// reverse_geocode.php
// Proxy untuk reverse geocode via OpenStreetMap Nominatim

header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');

$lat = isset($_GET['lat']) ? $_GET['lat'] : '';
$lon = isset($_GET['lon']) ? $_GET['lon'] : '';

if ($lat === '' || $lon === '') {
    echo json_encode(['error' => 'Missing latitude or longitude']);
    exit;
}

$url = "https://nominatim.openstreetmap.org/reverse?format=json&lat={$lat}&lon={$lon}&addressdetails=1";

$opts = [
    "http" => [
        "header" => "User-Agent: H Shop/1.0 (admin@apg-groups.com)\r\n"
    ]
];

$context = stream_context_create($opts);
$response = @file_get_contents($url, false, $context);

if ($response === false) {
    echo json_encode(['error' => 'Failed to contact OpenStreetMap API']);
    exit;
}

echo $response;
