<?php
// Permission checking is now handled by the permission system in auto-prepend.php
// This page is root-only (registered in $page_permissions in permission_manager.php
// and in $root_only_pages / $allowed_pages in index.php).

if (!is_root_user()) {
    http_response_code(403);
    echo "<div class='alert alert-danger'>Forbidden &mdash; root only.</div>";
    exit;
}

$settings_path = '/docker/whp/settings.json';
$settings = file_exists($settings_path) ? json_decode(file_get_contents($settings_path), true) : [];
if (!is_array($settings)) $settings = [];
$bo = $settings['boot_orchestrator'] ?? [];
$cw = $settings['customer_watchdog'] ?? [];

$last_boot_path = '/var/lib/whp/boot-orchestrator-last.json';
$last_boot = file_exists($last_boot_path) ? json_decode(file_get_contents($last_boot_path), true) : null;

$csrf_token = $_SESSION['csrf_token'] ?? '';

$saved = !empty($_GET['saved']);
?>
<div class="container-fluid">
  <?php /* page title is rendered by the layout from $allowed_pages[$page] */ ?>

  <?php if ($saved): ?>
    <div class="alert alert-success alert-dismissible fade show" role="alert">
      Settings saved successfully.
      <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
    </div>
  <?php endif; ?>

  <?php if ($last_boot): ?>
    <div class="card mb-4">
      <div class="card-header">Last boot</div>
      <div class="card-body">
        <p><strong>Booted at:</strong> <?= htmlspecialchars($last_boot['booted_at'] ?? '') ?>
           &nbsp;&nbsp;<strong>Duration:</strong> <?= (int)($last_boot['duration_sec'] ?? 0) ?>s</p>
        <pre><?= htmlspecialchars(json_encode($last_boot['tiers'] ?? [], JSON_PRETTY_PRINT)) ?></pre>
      </div>
    </div>
  <?php endif; ?>

  <form method="POST" action="/api/container-boot-save.php" class="row g-3">
    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrf_token) ?>">
    <h4 class="mt-3">Boot Orchestrator</h4>
    <div class="col-md-6">
      <label class="form-label">Boot orchestrator enabled</label>
      <select name="bo_enabled" class="form-select">
        <option value="1" <?= ($bo['enabled'] ?? true) ? 'selected' : '' ?>>On</option>
        <option value="0" <?= !($bo['enabled'] ?? true) ? 'selected' : '' ?>>Off (legacy)</option>
      </select>
    </div>
    <div class="col-md-3">
      <label class="form-label">Customer batch size</label>
      <input type="number" name="bo_batch_size" min="1" max="50" value="<?= (int)($bo['customer_batch_size'] ?? 5) ?>" class="form-control">
    </div>
    <div class="col-md-3">
      <label class="form-label">Customer batch delay (sec)</label>
      <input type="number" name="bo_batch_delay" min="1" max="120" value="<?= (int)($bo['customer_batch_delay_sec'] ?? 10) ?>" class="form-control">
    </div>
    <div class="col-md-3">
      <label class="form-label">Tier healthcheck timeout (sec)</label>
      <input type="number" name="bo_tier_timeout" min="10" max="300" value="<?= (int)($bo['tier_healthcheck_timeout_sec'] ?? 60) ?>" class="form-control">
    </div>
    <div class="col-md-3">
      <label class="form-label">Boot total timeout (sec)</label>
      <input type="number" name="bo_total_timeout" min="60" max="1800" value="<?= (int)($bo['boot_total_timeout_sec'] ?? 600) ?>" class="form-control">
    </div>
    <h4 class="mt-4">Customer Watchdog</h4>
    <div class="col-md-6">
      <label class="form-label">Watchdog enabled</label>
      <select name="cw_enabled" class="form-select">
        <option value="1" <?= ($cw['enabled'] ?? true) ? 'selected' : '' ?>>On</option>
        <option value="0" <?= !($cw['enabled'] ?? true) ? 'selected' : '' ?>>Off</option>
      </select>
    </div>
    <div class="col-md-3">
      <label class="form-label">Max restarts per tick</label>
      <input type="number" name="cw_max_per_tick" min="1" max="20" value="<?= (int)($cw['max_restarts_per_tick'] ?? 3) ?>" class="form-control">
    </div>
    <div class="col-md-3">
      <label class="form-label">Settle window (sec)</label>
      <input type="number" name="cw_settle" min="5" max="120" value="<?= (int)($cw['settle_window_sec'] ?? 30) ?>" class="form-control">
    </div>
    <div class="col-md-3">
      <label class="form-label">Post-start verify (sec)</label>
      <input type="number" name="cw_verify" min="1" max="60" value="<?= (int)($cw['post_start_verify_sec'] ?? 5) ?>" class="form-control">
    </div>
    <div class="col-md-3">
      <label class="form-label">Alert after N failures</label>
      <input type="number" name="cw_alert_after" min="2" max="20" value="<?= (int)($cw['alert_after_consecutive_failures'] ?? 5) ?>" class="form-control">
    </div>
    <div class="col-12 mt-3">
      <button type="submit" class="btn btn-primary">Save settings</button>
      <div class="form-text mt-2">Changes take effect on the next boot (orchestrator) or within 1 minute (watchdog).</div>
    </div>
  </form>
</div>
