<?php
/**
 * Returns the visitor's IP as seen by this origin (for shop-core fallback when client geo fails).
 * Same-origin GET/POST; JSON only.
 */
header('Content-Type: application/json; charset=UTF-8');
header('Cache-Control: no-store');

require_once __DIR__ . '/geoip-client-ip.inc.php';

if (!in_array($_SERVER['REQUEST_METHOD'] ?? '', ['GET', 'POST'], true)) {
    http_response_code(405);
    echo json_encode(['status' => 'error', 'message' => 'Method not allowed']);
    exit;
}

$ip = sb_geoip_client_ip();
if ($ip === '') {
    http_response_code(200);
    echo json_encode(['status' => 'error', 'message' => 'Could not determine client IP', 'ip' => '']);
    exit;
}

echo json_encode(['status' => 'ok', 'ip' => $ip], JSON_UNESCAPED_UNICODE);
