<?php
require_once '../../core/config.php';
require_once '../../core/session.php';
require_once '../../core/permissions.php';
require_once '../../services/geo/GoogleMapsService.php';

require_login();

header('Content-Type: application/json; charset=utf-8');

$q = isset($_GET['q']) ? trim($_GET['q']) : '';
$country = isset($_GET['country']) ? trim($_GET['country']) : 'cl';

try {
    $maps = new GoogleMapsService(null, $country);
    $results = $maps->autocomplete($q);
    echo json_encode(array(
        'success' => true,
        'configured' => $maps->isConfigured(),
        'query' => $q,
        'country' => $country,
        'count' => count($results),
        'results' => $results
    ), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} catch (Throwable $e) {
    echo json_encode(array('success' => false, 'configured' => false, 'results' => array(), 'message' => $e->getMessage()), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
