<?php
/** @var \BotGuard\Core\View $this @var \BotGuard\Core\Csrf $csrf @var array $thresholds @var array $actions */
$this->layout('layouts.app');
$human = $thresholds['human'] ?? ['min_score' => 80, 'action' => 'redirect_primary'];
$susp = $thresholds['suspicious'] ?? ['min_score' => 60, 'action' => 'redirect_alternate'];
$bot = $thresholds['bot'] ?? ['min_score' => 0, 'action' => 'block'];
$actionSelect = static function (string $name, string $current, array $actions): string {
    $html = '<select name="' . e($name) . '" class="form-select form-select-sm">';
    foreach ($actions as $a) {
        $sel = $a === $current ? ' selected' : '';
        $label = ucwords(str_replace('_', ' ', $a));
        $html .= '<option value="' . e($a) . '"' . $sel . '>' . e($label) . '</option>';
    }
    return $html . '</select>';
};
?>
<h1 class="h4 mb-1">Risk Thresholds</h1>
<p class="text-secondary small mb-4">A session scoring &ge; the Human minimum is Human; &ge; the Suspicious minimum is Suspicious; otherwise Bot.</p>

<form method="post" action="/admin/thresholds" style="max-width:640px">
    <?= $csrf->field() ?>
    <div class="card border-0 shadow-sm mb-3">
        <div class="card-body">
            <div class="row g-3 align-items-end mb-4">
                <div class="col-sm-4">
                    <label class="form-label text-success fw-semibold">Human minimum</label>
                    <input type="number" min="0" max="100" name="human_min" class="form-control" value="<?= e((int) $human['min_score']) ?>">
                </div>
                <div class="col-sm-4">
                    <label class="form-label small text-secondary">Action</label>
                    <?= $actionSelect('human_action', (string) $human['action'], $actions) ?>
                </div>
            </div>
            <div class="row g-3 align-items-end mb-4">
                <div class="col-sm-4">
                    <label class="form-label text-warning fw-semibold">Suspicious minimum</label>
                    <input type="number" min="0" max="100" name="suspicious_min" class="form-control" value="<?= e((int) $susp['min_score']) ?>">
                </div>
                <div class="col-sm-4">
                    <label class="form-label small text-secondary">Action</label>
                    <?= $actionSelect('suspicious_action', (string) $susp['action'], $actions) ?>
                </div>
            </div>
            <div class="row g-3 align-items-end">
                <div class="col-sm-4">
                    <label class="form-label text-danger fw-semibold">Bot</label>
                    <input type="number" class="form-control" value="0" disabled>
                </div>
                <div class="col-sm-4">
                    <label class="form-label small text-secondary">Action</label>
                    <?= $actionSelect('bot_action', (string) $bot['action'], $actions) ?>
                </div>
            </div>
        </div>
    </div>
    <button class="btn btn-primary" type="submit"><i class="bi bi-save"></i> Save thresholds</button>
</form>
