<?php
/**
 * Public verify page — reached from the QR code printed on a staff ID.
 *
 *   /Verify                → landing page with a search box
 *   /Verify/PGC-0012-A     → resolves the card number and shows a compact
 *                            status card the guard can eyeball in 2 seconds
 *
 * No auth. No PII beyond what's already on the printed card (name, photo,
 * position, licence class, validity). Deliberately spare — this page is
 * scanned quickly by a guard in daylight, not a manager at a desk.
 */
class VerifyController extends Controller
{
    /** Parses "PGC-{driverId}-{revision}" → [driverId, revision] or nulls. */
    private function parseCardNo(string $cardNo): array
    {
        $cardNo = strtoupper(trim($cardNo));
        if (!preg_match('/^PGC-(\d{1,6})-([A-Z])$/', $cardNo, $m)) {
            return [null, null];
        }
        return [(int)$m[1], $m[2]];
    }

    public function indexAction($cardNo = '')
    {
        $this->view->setLayout('l_verify');

        // Landing page (no card number typed / scanned)
        if ($cardNo === '') {
            $this->view->render('Verify/index', [
                'page_title' => 'Verify Staff ID · Pacific Group',
                'card_no'    => '',
                'status'     => 'idle',
                'driver'     => null,
            ]);
            return;
        }

        [$driverId, $rev] = $this->parseCardNo((string)$cardNo);
        if ($driverId === null) {
            $this->view->render('Verify/index', [
                'page_title' => 'Verify Staff ID · Pacific Group',
                'card_no'    => $cardNo,
                'status'     => 'malformed',
                'driver'     => null,
            ]);
            return;
        }

        $m = new Drivers();
        $d = $m->byId($driverId);
        if (!$d) {
            $this->view->render('Verify/index', [
                'page_title' => 'Verify Staff ID · Pacific Group',
                'card_no'    => $cardNo,
                'status'     => 'unknown',
                'driver'     => null,
            ]);
            return;
        }

        // Validity window mirrors PacificStaffIdPdf::validity() — the card
        // encodes today + 24 months, so the same window resolves here.
        // (In a future iter we'll persist issued_at/expires_at on the driver.)
        $expiresAt = new DateTimeImmutable('today +24 months');
        $expired   = $expiresAt < new DateTimeImmutable('today');
        $terminated= in_array($d['status'], ['terminated','suspended'], true);

        if ($terminated) {
            $status = 'revoked';
        } elseif ($expired) {
            $status = 'expired';
        } else {
            $status = 'valid';
        }

        $this->view->render('Verify/index', [
            'page_title' => 'Verify · ' . htmlspecialchars($d['full_name']) . ' · Pacific Group',
            'card_no'    => strtoupper($cardNo),
            'status'     => $status,
            'driver'     => $d,
            'expires_at' => $expiresAt->format('d M Y'),
        ]);
    }
}
