<?php
require_once __DIR__ . "/../../bootstrap.php";
require_once __DIR__ . '/../../src/AdminAuth.php';
AdminAuth::requireLogin();

$pageTitle = "Paramètres";

$hasPassword   = AdminAuth::hasPassword();
$maintenanceOn = file_exists(APP_ROOT . '/storage/maintenance.flag');

require_once APP_ROOT . '/src/Database.php';
require_once APP_ROOT . '/src/SmtpRelay.php';
require_once APP_ROOT . '/src/PeerTubeService.php';
$db = new Database();
$siteSettings  = $db->getAllSettings();
$peerTubeSettings   = PeerTubeService::getSettings($db);
$peerTubeEnabled    = PeerTubeService::isEnabled($db);
$peerTubeConfigured = PeerTubeService::isConfigured($db);
$smtpSettings   = SmtpRelay::getSettings($db);
$smtpConfigured  = SmtpRelay::isConfigured($db);
$smtpErrorField  = $_SESSION['_flash_smtp_field'] ?? null;
unset($_SESSION['_flash_smtp_field']);

// ── System section ────────────────────────────────────────────────────────────
require_once APP_ROOT . '/src/SystemCache.php';
require_once APP_ROOT . '/src/Controllers/SystemController.php';

$_db2        = new Database();
$_cache      = new SystemCache($_db2->getPDO());
$_controller = new SystemController($_db2, $_cache);

if (isset($_GET['refresh']) && $_GET['refresh'] === '1') {
    $_controller->invalidateAll();
}

$statusData     = $_controller->getStatusData();
$checks         = $statusData['checks'];
$statusCached   = $statusData['cached'];
$statusCacheAge = $statusData['cacheAge'];

$phpInfo  = $_controller->getPhpInfo();
$diskInfo = $_controller->getDiskInfo();

$diskTotal = $diskInfo['total'];
$diskFree  = $diskInfo['free'];
$diskUsed  = $diskInfo['used'];
$diskPct   = $diskInfo['pct'];
$diskColor = SystemController::diskColor($diskPct);

// ── Logs section ──────────────────────────────────────────────────────────────
$activeTab = $_GET['tab'] ?? 'nginx_access';
if ($activeTab === 'status') {
    $activeTab = 'nginx_access';
} elseif ($activeTab !== 'nginx_config' && !array_key_exists($activeTab, SystemController::LOG_FILES)) {
    $activeTab = 'nginx_access';
}

$selectedN = isset($_GET['n']) ? (int) $_GET['n'] : 100;
if (!in_array($selectedN, SystemController::ALLOWED_LINES, true)) {
    $selectedN = 100;
}

$logLines    = null;
$logError    = null;
$logFileMeta = null;

$nginxConfigLines  = null;
$nginxConfigSource = null;
$nginxConfigError  = null;
$nginxConfigMeta   = null;

if ($activeTab === 'nginx_config') {
    $nginxData         = $_controller->getNginxConfigData();
    $nginxConfigLines  = $nginxData['lines'];
    $nginxConfigSource = $nginxData['source'];
    $nginxConfigMeta   = $nginxData['meta'];
    $nginxConfigError  = $nginxData['error'];
} else {
    $logData     = $_controller->getLogData($activeTab, $selectedN);
    $logLines    = $logData['lines'];
    $logError    = $logData['error'];
    $logFileMeta = $logData['meta'];
}

$collapsed = $_COOKIE['sys_collapsed'] ?? null;
$statusInitiallyCollapsed = $collapsed === '1';

// ── Page setup ────────────────────────────────────────────────────────────────
if (empty($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}

$isAdmin = true; $bodyClass = 'admin-body';
$extraCss = ['/assets/css/system.css'];
require_once APP_ROOT . '/templates/head.php';
include APP_ROOT . '/templates/header.php';
include APP_ROOT . '/templates/admin/parametres.php';
require_once APP_ROOT . '/templates/admin/footer.php';
