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

enrep_api_require_key();

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

if ($clientnumber > 0) {
    $stmt = $GLOBALS['con']->prepare("
        SELECT `clientnumber`, `clientname`, `address1`, `address2`, `town`, `county`, `postcode`,
               `phone1`, `phone2`, `mobile`, `email`, `pri_contact_name`, `pri_contact_position`,
               `pri_contact_tel`, `pri_contact_mob`, `pri_contact_email`, `contract_number`, `ancillary`
        FROM `clientdetails`
        WHERE `clientnumber`=? AND `statuslive`=1
        LIMIT 1
    ");
    if (!$stmt) {
        enrep_api_fail(500, 'Client lookup unavailable');
    }
    $stmt->bind_param('i', $clientnumber);
} else {
    $stmt = $GLOBALS['con']->prepare("
        SELECT `clientnumber`, `clientname`, `address1`, `address2`, `town`, `county`, `postcode`,
               `phone1`, `phone2`, `mobile`, `email`, `pri_contact_name`, `pri_contact_position`,
               `pri_contact_tel`, `pri_contact_mob`, `pri_contact_email`, `contract_number`, `ancillary`
        FROM `clientdetails`
        WHERE `statuslive`=1
        ORDER BY `clientname` ASC, `clientnumber` ASC
        LIMIT ? OFFSET ?
    ");
    if (!$stmt) {
        enrep_api_fail(500, 'Client 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'],
        'client_name' => (string)$row['clientname'],
        'address' => [
            'address_line_1' => enrep_api_nullable_string($row['address1'] ?? ''),
            'address_line_2' => enrep_api_nullable_string($row['address2'] ?? ''),
            'town' => enrep_api_nullable_string($row['town'] ?? ''),
            'county' => enrep_api_nullable_string($row['county'] ?? ''),
            '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'] ?? ''),
        'primary_contact' => [
            'name' => enrep_api_nullable_string($row['pri_contact_name'] ?? ''),
            'position' => enrep_api_nullable_string($row['pri_contact_position'] ?? ''),
            'telephone' => enrep_api_nullable_string($row['pri_contact_tel'] ?? ''),
            'mobile' => enrep_api_nullable_string($row['pri_contact_mob'] ?? ''),
            'email' => enrep_api_nullable_string($row['pri_contact_email'] ?? ''),
        ],
        'contract_number' => enrep_api_nullable_string($row['contract_number'] ?? ''),
        'ancillary_reference' => enrep_api_nullable_string($row['ancillary'] ?? ''),
    ];
}

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