<?php
require_once __DIR__ . '/common.php';

$api = enrep_api_require_key();
$clientnumber = enrep_request_int('clientnumber', 0);
$locnumber = enrep_request_int('locnumber', 0);
$page = enrep_api_page();
$limit = (int)$page['per_page'];
$offset = (int)$page['offset'];

$rows = [];
if ($locnumber > 0 && $clientnumber > 0) {
    $stmt = $GLOBALS['con']->prepare("
        SELECT `clientnumber`, `locnumber`, `locname`, `locaddress1`, `locaddress2`, `locaddress3`, `locaddress4`,
               `postcode`, `phone1`, `phone2`, `mobile`, `email`, `contactlc`, `contactpm`, `contacttel`,
               `contactother`, `locnotes`, `locref`
        FROM `locations`
        WHERE `clientnumber`=? AND `locnumber`=? AND `statuslive`=1
        LIMIT 1
    ");
    if (!$stmt) {
        enrep_api_fail(500, 'Location lookup unavailable');
    }
    $stmt->bind_param('ii', $clientnumber, $locnumber);
} elseif ($locnumber > 0) {
    $stmt = $GLOBALS['con']->prepare("
        SELECT `clientnumber`, `locnumber`, `locname`, `locaddress1`, `locaddress2`, `locaddress3`, `locaddress4`,
               `postcode`, `phone1`, `phone2`, `mobile`, `email`, `contactlc`, `contactpm`, `contacttel`,
               `contactother`, `locnotes`, `locref`
        FROM `locations`
        WHERE `locnumber`=? AND `statuslive`=1
        LIMIT 1
    ");
    if (!$stmt) {
        enrep_api_fail(500, 'Location lookup unavailable');
    }
    $stmt->bind_param('i', $locnumber);
} elseif ($clientnumber > 0) {
    $stmt = $GLOBALS['con']->prepare("
        SELECT `clientnumber`, `locnumber`, `locname`, `locaddress1`, `locaddress2`, `locaddress3`, `locaddress4`,
               `postcode`, `phone1`, `phone2`, `mobile`, `email`, `contactlc`, `contactpm`, `contacttel`,
               `contactother`, `locnotes`, `locref`
        FROM `locations`
        WHERE `clientnumber`=? AND `statuslive`=1
        ORDER BY `locname` ASC, `locnumber` ASC
        LIMIT ? OFFSET ?
    ");
    if (!$stmt) {
        enrep_api_fail(500, 'Location lookup unavailable');
    }
    $stmt->bind_param('iii', $clientnumber, $limit, $offset);
} else {
    $stmt = $GLOBALS['con']->prepare("
        SELECT `clientnumber`, `locnumber`, `locname`, `locaddress1`, `locaddress2`, `locaddress3`, `locaddress4`,
               `postcode`, `phone1`, `phone2`, `mobile`, `email`, `contactlc`, `contactpm`, `contacttel`,
               `contactother`, `locnotes`, `locref`
        FROM `locations`
        WHERE `statuslive`=1
        ORDER BY `locname` ASC, `locnumber` ASC
        LIMIT ? OFFSET ?
    ");
    if (!$stmt) {
        enrep_api_fail(500, 'Location lookup unavailable');
    }
    $stmt->bind_param('ii', $limit, $offset);
}

$stmt->execute();
$result = $stmt->get_result();
$stmt->close();

while ($row = mysqli_fetch_assoc($result)) {
    $rows[] = [
        'clientnumber' => (int)$row['clientnumber'],
        'locnumber' => (int)$row['locnumber'],
        'site_name' => enrep_api_nullable_string($row['locname'] ?? ''),
        'site_reference' => enrep_api_nullable_string($row['locref'] ?? ''),
        'address' => [
            'address_line_1' => enrep_api_nullable_string($row['locaddress1'] ?? ''),
            'address_line_2' => enrep_api_nullable_string($row['locaddress2'] ?? ''),
            'address_line_3' => enrep_api_nullable_string($row['locaddress3'] ?? ''),
            'address_line_4' => enrep_api_nullable_string($row['locaddress4'] ?? ''),
            'postcode' => enrep_api_nullable_string($row['postcode'] ?? ''),
        ],
        'telephone' => enrep_api_nullable_string($row['phone1'] ?? ''),
        'secondary_telephone' => enrep_api_nullable_string($row['phone2'] ?? ''),
        'mobile' => enrep_api_nullable_string($row['mobile'] ?? ''),
        'email' => enrep_api_nullable_string($row['email'] ?? ''),
        'contacts' => [
            'local_contact' => enrep_api_nullable_string($row['contactlc'] ?? ''),
            'property_manager' => enrep_api_nullable_string($row['contactpm'] ?? ''),
            'contact_telephone' => enrep_api_nullable_string($row['contacttel'] ?? ''),
            'other_contact' => enrep_api_nullable_string($row['contactother'] ?? ''),
        ],
        'notes' => enrep_api_nullable_string($row['locnotes'] ?? ''),
    ];
}

enrep_api_json([
    'ok' => true,
    'data' => $locnumber > 0 ? ($rows[0] ?? null) : $rows,
    'meta' => $locnumber > 0 ? null : enrep_api_meta($page, count($rows)),
]);
