<?php

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

$base = 'https://' . $_SERVER['SERVER_NAME'] . explode('?', $_SERVER['REQUEST_URI'])[0];

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;
}

function jenc($data): string {
    return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}

$info = $_SERVER['PATH_INFO'];
if ($info !== '') {
    $id = substr($info, 1);

    $url = "https://www.bioc.info/search/producerdetail?producer=" . urlencode($id);
    $s = curl_init($url);
    curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
    if (($html = curl_exec($s)) === false) {
        header('Status: 500');
        header('Content-Length: 34');
        echo "{\"error\":\"internal server error\"}\n";
        exit;
    }
    $p1 = strpos($html, '<main>');
    $p2 = strpos($html, '</main>');
    if ($p1 === false || $p2 === false) {
        header('Status: 404');
        header('Content-Length: 22');
        echo "{\"error\":\"not found\"}\n";
        exit;
    }
    $html = substr($html, $p1 + 6, $p2 - $p1 - 6);

    $data = [];
    preg_match_all('@<div class="row">\s*<div class="col[^>]*?>\s*(.*?)\s*</div>\s*<div class="col[^>]*?>\s*(.*?)\s*</div>\s*</div>@s', $html, $matches, PREG_SET_ORDER);
    foreach ($matches as $m) {
        $data[$m[1]] = trim(preg_replace('/\s*\n\s*/', "\n", html_entity_decode(preg_replace('/<.*?>/', '', str_replace('<br/>', "\n", preg_replace('/\s+/', ' ', trim($m[2])))))));
    }

    $certRows = [];
    preg_match_all('@<tr>\s*<td.*?href="([^"]*)">\s*(.*?)\s*</td>\s*<td>\s*(.*?)\s*</td>\s*<td>\s*(.*?)\s*</td>\s*<td>\s*(.*?)\s*</td>\s*</tr>@s', $html, $matches, PREG_SET_ORDER);
    foreach ($matches as $m) {
        $cert = [];
        foreach (array_splice($m, 1) as $s) {
            $cert[] = trim(preg_replace('/\s*\n\s*/', "\n", preg_replace('/<.*?>/', '', str_replace('<br/>', "\n", preg_replace('/\s+/', ' ', trim(html_entity_decode($s)))))));
        }
        $certRows[] = $cert;
    }

    $lines = explode("\n", $data['Address']);
    [$cc, $_, $postalCode, $city] = explode(' ', $lines[2], 4);

    $certs = [];
    foreach ($certRows as $row) {
        [$nr, $lang] = explode('(', $row[1], 2);
        [$from, $to] = explode(' - ', $row[3], 2);
        $certs[] = [
            'nr' => trim($nr),
            'language' => substr($lang, 0, 2),
            'pdfUrl' => $row[0],
            'type' => $row[2],
            'validFrom' => date('Y-m-d', strtotime($from)),
            'validUntil' => date('Y-m-d', strtotime($to)),
        ];
    }

    [$authCode, $authName] = explode(' ', $data['Certification body'], 2);
    echo jenc([
        'id' => $id,
        'idNr' => $data['Operator ID'] ?? null,
        'name' => $lines[0],
        'address' => $lines[1],
        'postalCode' => $postalCode,
        'city' => $city,
        'countryCode' => $cc,
        'authority' => ['code' => $authCode, 'name' => $authName],
        'url' => $base,
        'originUrl' => $url,
        'certificates' => $certs,
    ]);
    exit;
}

$source_groups = [
    ['', '0892fdd7-6649-cb3b-75ef-a60649cfebfd'],
    ['', 'fde4ccef-0f36-4db9-9f2f-9bb6009c872a'],
    ['', '8e6d20d3-6bbe-c7ae-7a77-fb7fa80ee291'],
    ['', '830b3bd7-f1da-e20e-300d-e561bfb8205b'],
    ['', 'ed025d59-056f-ffd0-79e0-05348fe88d59'],
    ['', '95d536c8-c869-9f72-1bce-4256a2a4ce49'],
    ['',
        'bef6b49a-3186-ba7f-fffb-281bfb4ca2d2', 'fc7a0cc3-c4b4-a4fb-5b7e-5cf9b68ed198',
        '9aecd829-8f3c-6829-449f-9cb511e0c1ff', 'e7a7089b-0fd7-60b9-48af-c0988a90ec37',
        '63d570d8-0ab2-b793-fe11-62e0777e4236'
    ],
];

$country = [
    'AT' => 'a1e51f85-27b5-a1e3-65f7-561569884d38',
    'DE' => '1be4d468-ba47-08f1-f048-f45869af856f',
][$_GET['country'] ?? null] ?? null;
$postalCode = $_GET['postalCode'] ?? null;
$name = $_GET['name'] ?? null;
$idNr = $_GET['idNr'] ?? null;

if ($country === null) {
    header('Status: 400');
    header('Content-Length: 88');
    echo "{\"error\":\"bad request\",\"message\":\"The 'country' request URL query parameter is required\"}\n";
    exit;
}

$data = [];

$origin = "https://www.bioc.info/search/producersearchresult?producerSearch%5Bcountry%5D={$country}&producerSearch%5Bcitycode%5D=" . urlencode($postalCode) . "&producerSearch%5Bname%5D=" . urlencode($name) . "&producerSearch%5BoperatorId%5D=" . urlencode($idNr);
$url = "https://www.bioc.info/search/producerSearchQuery?search[country]={$country}&search[citycode]=" . urlencode($postalCode) . "&search[name]=" . urlencode($name) . "&search[operatorId]=" . urlencode($idNr);
$m = curl_multi_init();
$requests = [];
foreach ($source_groups as $group) {
    $s = curl_init($url . implode('&sources[]=', $group));
    $requests[] = $s;
    curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
    curl_multi_add_handle($m, $s);
}

$running = 1;
do {
    curl_multi_exec($m, $running);
    if ($running) usleep(250_000);
} while ($running);
foreach ($requests as $r) {
    curl_multi_remove_handle($m, $r);
}
curl_multi_close($m);

foreach ($requests as $s) {
    if (($json = curl_multi_getcontent($s)) === null) {
        header('Status: 500');
        header('Content-Length: 34');
        echo "{\"error\":\"internal server error\"}\n";
        exit;
    }
    $res = json_decode($json, true)['results'];
    foreach ($res as $r) {
        $data[$r['id']] = $r['row'];
    }
}

$first = true;
echo "{\"originUrl\":\"$origin\",\"data\":[\n";
foreach ($data as $id => $row) {
    if (!$first) echo ",\n";
    $row = explode("</", $row);
    $auth = explode(' ', substr($row[3], strrpos($row[3], '>') + 1), 2);
    echo "  ";
    echo jenc([
        'id' => $id,
        'name' => substr($row[0], strrpos($row[0], '>') + 1),
        'postalCode' => substr($row[1], strrpos($row[1], '>') + 1),
        'city' => substr($row[2], strrpos($row[2], '>') + 1),
        'authority' => ['code' => $auth[0], 'name' => $auth[1]],
        'url' => "$base/$id",
    ]);
    $first = false;
}
echo "\n]}\n";

