<?php
require_once __DIR__ . '/../../api-user-check.php';
require_once __DIR__ . '/../../php/locations/locations.php';

$query = $_GET['name'] ?? false;
$realm = $_GET['realm'] ?? 'all';
$maxResults = $_GET['max'] ?? false;

$lat = null;
$long = null;

if (isset($_SERVER['HTTP_X_LOCATION'])) {
    $location = $_SERVER['HTTP_X_LOCATION'];
    $locationObj = json_decode($location);
    if (isset($locationObj->latitude) && isset($locationObj->longitude)) {
        $lat = $locationObj->latitude;
        $long = $locationObj->longitude;
    }
}
$matches = Locations::search($query, $realm, $maxResults, $lat, $long);

header('Content-Type: application/json');
echo json_encode($matches);
