<?php
/**
 * InstallController — the public side of the mobile install flow.
 *
 *   GET  /Install/{token}       → shows the driver info form (name + phone)
 *   POST /Install/{token}/save  → records driver info, redirects to apk
 *   GET  /Install/{token}/apk   → streams the APK file
 *
 * Registered in Router::PUBLIC_CONTROLLERS. The token itself is the gate —
 * no session required, driver hits this from their phone browser.
 */
class InstallController extends Controller
{
    public function indexAction($token = '')
    {
        $m   = new AppInstallTokens();
        $row = $m->byToken(trim((string)$token));
        $status = $m->status($row);

        $this->view->setLayout('l_install');
        $this->view->render('Install/index', [
            'page_title' => 'Install Pacific Driver',
            'token'      => $token,
            'row'        => $row,
            'status'     => $status,
            'apk_meta'   => $m->apkMeta(),
        ]);
    }

    public function saveAction($token = '')
    {
        if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') { Router::redirect('Install/' . urlencode($token)); return; }

        $m    = new AppInstallTokens();
        $row  = $m->byToken(trim((string)$token));
        if ($m->status($row) !== 'ok') {
            Router::redirect('Install/' . urlencode($token));
            return;
        }

        $name  = trim((string)($_POST['name']  ?? ''));
        $phone = trim((string)($_POST['phone'] ?? ''));
        $empNo = trim((string)($_POST['employee_no'] ?? ''));

        if ($name === '' || $phone === '') {
            $this->view->setLayout('l_install');
            $this->view->render('Install/index', [
                'page_title' => 'Install Pacific Driver',
                'token'      => $token,
                'row'        => $row,
                'status'     => 'ok',
                'apk_meta'   => $m->apkMeta(),
                'fb_msg'     => '<div class="alert alert-danger">Full name and phone are required.</div>',
                'form'       => $_POST,
            ]);
            return;
        }

        $m->recordDownload((int)$row['c0d'], ['name' => $name, 'phone' => $phone, 'employee_no' => $empNo]);
        // Send them to the APK stream — the browser starts the download.
        Router::redirect('Install/apk/' . $token);
    }

    public function apkAction($token = '')
    {
        $m   = new AppInstallTokens();
        $row = $m->byToken(trim((string)$token));
        if ($m->status($row) !== 'ok') {
            $this->view->setLayout('l_install');
            $this->view->render('Install/index', [
                'page_title' => 'Install Pacific Driver',
                'token'      => $token,
                'row'        => $row,
                'status'     => $m->status($row),
                'apk_meta'   => $m->apkMeta(),
            ]);
            return;
        }
        $path = $m->apkPath();
        if (!$path) {
            $this->view->setLayout('l_install');
            $this->view->render('Install/index', [
                'page_title' => 'Install Pacific Driver',
                'token'      => $token,
                'row'        => $row,
                'status'     => 'no_apk',
                'apk_meta'   => $m->apkMeta(),
            ]);
            return;
        }

        if (ob_get_length()) ob_clean();
        header('Content-Type: application/vnd.android.package-archive');
        header('Content-Disposition: attachment; filename="pacific-driver.apk"');
        header('Content-Length: ' . filesize($path));
        header('X-Content-Type-Options: nosniff');
        readfile($path);
        exit();
    }
}
