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

// Raw camera payload — contains the snapshots that parseInput() doesn't expose.
$raw       = json_decode(file_get_contents('php://input'), true) ?: [];
$cropB64   = $raw['license_plate_snapshot'] ?? '';
$fullB64   = $raw['full_snapshot']          ?? '';

$now       = date('YmdHis');
$dayname   = strtolower(date('l'));
$time      = date('Hi');
$input     = parseInput();
$plate     = $input['plate'];
$direction = $input['direction'];
$device    = $input['device'];

$log = implode(';', [$now, $plate, $direction, $device]);

if ($time < '0700' || $time > '2359') {
    writeLog('rijsdaal.txt', $log . ";$dayname;outOfOfficeHours;noaction");
    die();
}

$log .= ";$dayname;$time";

if ($direction !== 'Approach') {
    writeLog('rijsdaal.txt', $log . ';no action');
    die();
}

$conn = dbConnect();
$stmt = mysqli_prepare($conn, 'SELECT 1 FROM lp WHERE lpcontent = ? AND lplocation = 2 LIMIT 1');
mysqli_stmt_bind_param($stmt, 's', $plate);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$allowed = mysqli_stmt_num_rows($stmt) > 0;
mysqli_stmt_close($stmt);

$relayUrl = 'http://172.17.13.40/leds.cgi?state=P0010&led=0';

if ($allowed) {
    // Known plate — always let through
    $status = 'allowed';
    $log   .= ';ALLOWED';
    $log   .= ';' . triggerRelay($relayUrl);
} elseif (deviceDirection($device) === 'exit' && getSetting('kolmen_allow_unknown_exit', '1') === '1') {
    // Unknown plate at an exit barrier (e.g. WijerUIT) — allowed out when the
    // "allow unknown exit" setting is enabled.
    $status = 'exit';
    $log   .= ';UnknownExit';
    $log   .= ';' . triggerRelay($relayUrl);
} else {
    $status = 'denied';
    $log   .= ';NOT allowed';
}

$ins = mysqli_prepare($conn, 'INSERT INTO logs (time, lp, device, status) VALUES (?, ?, ?, ?)');
mysqli_stmt_bind_param($ins, 'ssss', $now, $plate, $device, $status);
$ok    = mysqli_stmt_execute($ins);
$logid = $ok ? mysqli_insert_id($conn) : 0;
$log  .= $ok ? ';dbok' : ';' . mysqli_error($conn);
mysqli_stmt_close($ins);

mysqli_close($conn);

saveEventSnapshots($logid, $cropB64, $fullB64);
writeLog('rijsdaal.txt', $log);

/**
 * Resolve a camera's barrier role from the devices table: 'entry' or 'exit'.
 * Falls back to the legacy name convention (…UIT = exit) when unset.
 */
function deviceDirection(string $device): string {
    $conn = dbConnect();
    $stmt = mysqli_prepare($conn, 'SELECT direction FROM devices WHERE device_name = ? LIMIT 1');
    mysqli_stmt_bind_param($stmt, 's', $device);
    mysqli_stmt_execute($stmt);
    $row = mysqli_fetch_row(mysqli_stmt_get_result($stmt));
    mysqli_close($conn);

    $dir = $row[0] ?? null;
    if ($dir === 'entry' || $dir === 'exit') return $dir;
    return str_ends_with($device, 'UIT') ? 'exit' : 'entry';
}

/**
 * Decode the base64 JPEGs the Milesight camera POSTs and store them keyed by log id.
 * Same format/paths as kolmen.php on the Stevoort side, so admin/snapshot.php and
 * misreads.php (Plate Review) work identically here.
 *   admin/data/snap/<logid div 1000>/<logid>.jpg        full frame
 *   admin/data/snap/<logid div 1000>/<logid>_crop.jpg   plate crop
 */
function saveEventSnapshots(int $logid, string $cropB64, string $fullB64): void {
    if ($logid <= 0) return;
    $shard = intdiv($logid, 1000);
    $dir   = __DIR__ . "/admin/data/snap/$shard";
    if (!is_dir($dir) && !@mkdir($dir, 0775, true) && !is_dir($dir)) return;

    foreach (['' => $fullB64, '_crop' => $cropB64] as $suffix => $b64) {
        if ($b64 === '' || $b64 === '-') continue;
        $bin = base64_decode(preg_replace('/\s+/', '', $b64));
        if ($bin === false || strlen($bin) < 100) continue;
        if (substr($bin, 0, 2) !== "\xFF\xD8") continue;   // must start with JPEG SOI
        @file_put_contents("$dir/{$logid}{$suffix}.jpg", $bin);
    }
}
