<?php

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

use AnthonyMartin\GeoLocation\GeoLocation as GeoLocation;

$userlocation = GeoLocation::fromDegrees($VARS["latitude"], $VARS["longitude"]);

$radius = 2;
if (!empty($VARS["radius"])) {
    $radius = min(10.0, $VARS["radius"] * 1.0);
}
$searchbounds = $userlocation->boundingCoordinates($radius, "miles");

ob_flush();
$people = $database->debug()->select("merchants", [
    "merchantid",
    "name",
    "address",
    "latitude",
    "longitude"
        ], [
    "AND" => [
        'latitude[<>]' => [$searchbounds[0]->getLatitudeInDegrees(), $searchbounds[1]->getLatitudeInDegrees()],
        'longitude[<>]' => [$searchbounds[0]->getLongitudeInDegrees(), $searchbounds[1]->getLongitudeInDegrees()]
    ],
    "LIMIT" => 100
        ]
);
$query = ob_get_contents();
ob_clean();
$merchants = $database->query($query)->fetchAll();

// Get a list of the merchant types
for ($i = 0; $i < count($merchants); $i++) {
    $types = $database->select(
            "merchant_type",
            ["[>]merchant_types" => "typeid"],
            ["merchant_type.typeid (id)", "type (name)", "icon"],
            ["merchantid" => $merchants[$i]["merchantid"]]
    );
    $merchants[$i]["types"] = $types;
}

if (!empty($VARS["format"]) && $VARS["format"] == "geojson") {
    $geojson = [
        "name" => "Nearby Merchants",
        "type" => "FeatureCollection",
        "features" => []
    ];

    foreach ($merchants as $merchant) {
        $geojson["features"][] = [
            "type" => "Feature",
            "geometry" => [
                "type" => "Point",
                "coordinates" => [
                    $merchant["longitude"] * 1.0,
                    $merchant["latitude"] * 1.0
                ]
            ],
            "properties" => [
                "id" => $merchant["merchantid"],
                "name" => utf8_encode($merchant["name"]),
                "address" => utf8_encode($merchant["address"]),
                "types" => $merchant["types"]
            ]
        ];
    }

    exitWithJson($geojson);
}

$nearby = [];

foreach ($merchants as $merchant) {
    $nearby[] = [
        "name" => utf8_encode($merchant["name"]),
        "merchantid" => $merchant["merchantid"],
        "address" => utf8_encode($merchant["address"]),
        "types" => $merchant["types"],
        "latitude" => $merchant["latitude"] * 1.0,
        "longitude" => $merchant["longitude"] * 1.0
    ];
}

exitWithJson([
    "status" => "OK",
    "radius" => $radius,
    "bounds" => [
        0 => [
            "latitude" => $searchbounds[0]->getLatitudeInDegrees(),
            "longitude" => $searchbounds[0]->getLongitudeInDegrees()
        ],
        1 => [
            "latitude" => $searchbounds[1]->getLatitudeInDegrees(),
            "longitude" => $searchbounds[1]->getLongitudeInDegrees()
        ],
    ],
    "nearby" => $nearby
]);
