include_once 'config.php';
require_once '../cps_api.php';
$config['database'] = 'public::osm_poi';
try {
    // creating a CPS_Connection instance
    $cpsConnection = new CPS_Connection($config['connection'], $config['database'], $config['username'], $config['password'], 'document', '//document/id', array('account' => $config['account']));
    $cpsConnection->setDebug(1);
    $offset = 0;
    $docs = 30;
    $listXml = '<id>yes</id>  <lat>yes</lat>  <lon>yes</lon>';
    $query = '{' . CPS_Term(" ><exampleshape1") . CPS_Term(" ><exampleshape2") . '}';
    //  $query = CPS_Term(" ><exampleshape2");
    //  $query = CPS_Term('cars', 'category') . CPS_Term('>=2010', 'car_params/year');
    $searchRequest = new CPS_SearchRequest($query, $offset, $docs);
    $searchRequest->setParam('list', $listXml);
    $shape1 = CPS_CircleDefinition("exampleshape1", array(48.060736, -2.6902734), '1 km', 'lat', 'lon');
    $shape2 = CPS_PolygonDefinition("exampleshape2", array(array(50.5735874, 9.997238299999999), array(50.574689, 10.006046), array(50.564493, 10.008982)), 'lat', 'lon');
    $shapes = '<shapes>
             <exampleshape1>
                <center>48.060736 -2.6902734</center>
                <radius>1 km</radius>
                <coord1_tag_name>lat</coord1_tag_name>
                <coord2_tag_name>lon</coord2_tag_name>
            </exampleshape1>
            <exampleshape2>50.5735874 9.9972383; 50.574689 10.006046; 50.564493 10.008982;
                <coord1_tag_name>lat</coord1_tag_name>
                <coord2_tag_name>lon</coord2_tag_name>
            </exampleshape2>
            </shapes>';
    ////<coord1_tag_name>lat</coord1_tag_name>
    ////<coord2_tag_name>lon</coord2_tag_name>
 $amenities = (array) $app->request->get('amenities', []);
 // amenities filter
 $page = (int) $app->request->get('page', 1);
 // page
 if ($page < 1) {
     $page = 1;
 }
 // structure of response
 $objects = ['total' => 0, 'from' => 0, 'to' => 0, 'pages' => 0, 'current_page' => 0, 'list' => [], 'amenities' => []];
 $cps_connection = new CPS_Connection(new CPS_LoadBalancer($config['connection']), $config['database'], $config['username'], $config['password'], 'document', '//document/id', ['account' => $config['account']]);
 $docs = 50;
 $offset = $docs * ($page - 1);
 $radius = $radius / 1000;
 // radius is received in meters, but request to Clusterpoint should be in km
 // Shape (circle) definition. This will need to be attached to search query.
 $circle = CPS_CircleDefinition('circle', [$lat, $lng], $radius . ' km', 'lat', 'lon');
 $query = CPS_Term(' ><circle');
 // search inside of defined circle
 if ($q !== '') {
     $query .= CPS_Term($q);
     // if there was search phrase, attach it to the query
 }
 $query_without_amenities = $query;
 $aggregate = 'DISTINCT tags.amenity';
 // aggregate distinct values of amenity tag
 if (count($amenities)) {
     if (array_search('-', $amenities) !== false) {
         // if there is amenity with value "-", this means that "Amenity is not set" was selected
         // lets replace - with ="" which tells Clusterpoint to look for documents for which this tag does not exist
         $amenities[array_search('-', $amenities)] = '=""';
     }