<?php

declare(strict_types=1);

/**
 * Caller terminal page (SPEC §7): one canvas, one hidden input, zero
 * framework. All logic lives in assets/term.js + assets/cp437render.js;
 * this page only injects board config (relative URLs — subdirectory-safe §2.7).
 */

// Old-PHP guard (this file stays PHP 7-parseable so it can actually run):
// send the caller to the installer's plain explanation, never a blank 500.
if (PHP_VERSION_ID < 80100) {
    header('Location: install.php');
    exit;
}

require __DIR__ . '/lib/bootstrap.php';

if (!atdt_installed()) {
    header('Location: install.php');
    exit;
}
atdt_migrate();

$settings = atdt_settings();
$boardName = $settings->get('board_name', 'ATDT') ?? 'ATDT';
// The "board number" the ceremony dials (§7.6): the sysop's own number
// when one is set (1.1.0, admin settings), else derived from the board
// name -- stable, and formatted like a local number from the golden era.
$digits = sprintf('%07u', crc32($boardName) % 10000000);
$dial = trim((string)$settings->get('board_phone', ''));
if ($dial === '') {
    $dial = substr($digits, 0, 3) . '-' . substr($digits, 3);
}
// ?preview=MENU: read-only console preview (§11.5); api.php gates it to the
// admin session, this page only passes the request through.
$preview = strtoupper((string)($_GET['preview'] ?? ''));
$preview = preg_match('/^[A-Z0-9_]{1,20}$/', $preview) ? $preview : null;
$config = [
    'api' => 'api.php',
    'font' => atdt_asset('assets/fonts/Web437_IBM_VGA_8x16.woff'),
    'ceremony' => $preview === null && $settings->bool('connect_ceremony', true),
    'dial' => $dial,
    'baud' => (int)($settings->get('default_baud', '9600') ?? 9600), // 1.6.0: the line speed before login
    'preview' => $preview,
];

header('Content-Type: text/html; charset=utf-8');
// Never let a browser keep this page: it carries the versioned asset URLs
// that make upgrades actually reach callers (owner round 6).
header('Cache-Control: no-store');
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title><?= htmlspecialchars($boardName, ENT_QUOTES) ?></title>
<link rel="stylesheet" href="<?= htmlspecialchars(atdt_asset('assets/term.css'), ENT_QUOTES) ?>">
</head>
<body>
<canvas id="term" width="640" height="400"></canvas>
<input id="kbd" type="text" autocomplete="off" autocapitalize="off" autocorrect="off" spellcheck="false" aria-hidden="true" tabindex="-1">
<div id="keyrow">
    <button type="button" data-key="ESC">ESC</button>
    <button type="button" data-key="UP">&uarr;</button>
    <button type="button" data-key="DOWN">&darr;</button>
    <button type="button" data-key="LEFT">&larr;</button>
    <button type="button" data-key="RIGHT">&rarr;</button>
    <button type="button" data-key="TAB">TAB</button>
    <button type="button" data-key="ENTER">ENTER</button>
    <button type="button" id="fullkeys" title="Full screen. The browser hands every key to the board, Ctrl W and Ctrl T included. Hold Esc to leave.">&#x26F6; FULL</button>
</div>
<noscript><pre>This board needs JavaScript to place the call.</pre></noscript>
<script id="atdt-config" type="application/json"><?= json_encode($config, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG) ?></script>
<script src="<?= htmlspecialchars(atdt_asset('assets/cp437render.js'), ENT_QUOTES) ?>"></script>
<script src="<?= htmlspecialchars(atdt_asset('assets/msged.js'), ENT_QUOTES) ?>"></script>
<script src="<?= htmlspecialchars(atdt_asset('assets/term.js'), ENT_QUOTES) ?>"></script>
<script>window.atdtTerm = AtdtTerm.boot(document);</script>
</body>
</html>
