<?php

header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json; charset=UTF-8');

if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'HEAD') {
    header('Status: 405');
    header('Content-Length: 56');
    header('Allow: GET, HEAD');
    echo "{\"error\":\"method not allowed\",\"allow\":[\"GET\",\"HEAD\"]}\n";
    exit;
}

$info = $_SERVER['PATH_INFO'];
if ($info !== '') {
    header('Status: 404');
    header('Content-Length: 22');
    echo "{\"error\":\"not found\"}\n";
    exit;
}

$query = [];
if (isset($_GET['country'])) {
    $query[] = 'country=' . [
        'AT' => '1',
        'DE' => '2',
    ][$_GET['country']] ?? '';
}
if (isset($_GET['postalCode'])) {
    $query[] = 'zipcode=' . urlencode($_GET['postalCode']);
}
if (isset($_GET['name'])) {
    $query[] = 'aname=' . urlencode($_GET['name']);
}
if (isset($_GET['idNr'])) {
    $query[] = 'clientcode=' . urlencode($_GET['idNr']);
}
if (isset($_GET['lfbisNr'])) {
    $query[] = 'lfbis=' . urlencode($_GET['lfbisNr']);
}

echo "{\"data\":";
passthru("python3 .operators.py " . escapeshellarg(implode('&', $query)));
echo "}\n";
