<?php
require_once __DIR__ . '/../app/bootstrap.php';

$envPath = base_path('.env');
$effective = function_exists('env') ? (string) env('EXECUTIVE_DASHBOARD_MODE', 'fast') : 'fast';
$rawLines = [];
if (is_file($envPath)) {
    foreach (file($envPath, FILE_IGNORE_NEW_LINES) ?: [] as $lineNo => $line) {
        if (stripos($line, 'EXECUTIVE_DASHBOARD_MODE') !== false) {
            $rawLines[] = ['line' => $lineNo + 1, 'text' => $line, 'commented' => preg_match('/^\s*#/', $line) === 1];
        }
    }
}

$normalized = strtolower(trim($effective, "\"' "));
if (strpos($normalized, 'live') === 0) { $normalized = 'live'; }
elseif (strpos($normalized, 'fast') === 0) { $normalized = 'fast'; }
else { $normalized = 'fast'; }

header('Content-Type: text/html; charset=utf-8');
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Executive Mode Check</title>
<style>
body{font-family:Arial,Tahoma,sans-serif;background:#f6f8fb;color:#111827;margin:0;padding:32px} .card{max-width:980px;background:#fff;border:1px solid #e5e7eb;border-radius:18px;padding:24px;box-shadow:0 12px 32px rgba(15,23,42,.08)}
h1{margin:0 0 12px}.ok{color:#0f9f8f}.warn{color:#b45309}.bad{color:#dc2626}pre{background:#0f172a;color:#e5e7eb;padding:14px;border-radius:12px;overflow:auto}.pill{display:inline-block;padding:8px 12px;border-radius:999px;background:#eef2ff;color:#4c1d95;font-weight:bold}.line{padding:10px;border:1px solid #e5e7eb;border-radius:10px;margin:8px 0}.comment{background:#fff7ed}.active{background:#ecfdf5}
</style>
</head>
<body>
<div class="card">
<h1>Executive Dashboard Mode Check</h1>
<p>Effective mode from env(): <span class="pill"><?= htmlspecialchars($effective, ENT_QUOTES, 'UTF-8') ?></span></p>
<p>Normalized mode used by repository: <span class="pill"><?= htmlspecialchars($normalized, ENT_QUOTES, 'UTF-8') ?></span></p>
<?php if ($normalized === 'live'): ?>
<p class="warn"><b>Warning:</b> Direct live mode may be slow until the KPI summary/cache layer is created.</p>
<?php else: ?>
<p class="ok"><b>Fast mode is active.</b> If your .env line starts with #, it is a comment and will be ignored.</p>
<?php endif; ?>
<h2>Matching .env lines</h2>
<?php if (!$rawLines): ?>
<p class="bad">No EXECUTIVE_DASHBOARD_MODE line found in .env.</p>
<?php else: foreach ($rawLines as $row): ?>
<div class="line <?= $row['commented'] ? 'comment' : 'active' ?>">
<b>Line <?= (int)$row['line'] ?>:</b>
<code><?= htmlspecialchars($row['text'], ENT_QUOTES, 'UTF-8') ?></code>
<br>
Status: <?= $row['commented'] ? '<span class="warn">COMMENTED / ignored</span>' : '<span class="ok">ACTIVE</span>' ?>
</div>
<?php endforeach; endif; ?>
<h2>Correct examples</h2>
<pre>EXECUTIVE_DASHBOARD_MODE=fast</pre>
<pre>EXECUTIVE_DASHBOARD_MODE=live</pre>
<h2>Wrong example</h2>
<pre># EXECUTIVE_DASHBOARD_MODE=live</pre>
<p>After changing .env, restart Apache and refresh with Ctrl+F5.</p>
</div>
</body>
</html>
