<?php
require_once '../includes/auth.php';
requireRole('super_admin');

$logDir = '../logs';
$logFiles = [];

if (is_dir($logDir)) {
    $files = scandir($logDir, SCANDIR_SORT_DESCENDING);
    foreach ($files as $file) {
        if (strpos($file, 'pesapal_callback') === 0 || strpos($file, 'ussd') === 0) {
            $logFiles[] = $file;
        }
    }
}

$selectedLog = $_GET['log'] ?? ($logFiles[0] ?? null);
$logContent = '';

if ($selectedLog && in_array($selectedLog, $logFiles)) {
    $logContent = file_get_contents($logDir . '/' . $selectedLog);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>M-Pesa Test Logs - Shinda</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="dashboard.php">Shinda Admin</a>
            <div class="navbar-collapse">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item"><a class="nav-link" href="dashboard.php">Dashboard</a></li>
                    <li class="nav-item"><a class="nav-link" href="users.php">Users</a></li>
                    <li class="nav-item"><a class="nav-link active" href="logs.php">Logs</a></li>
                    <li class="nav-item"><a class="nav-link" href="../logout.php">Logout</a></li>
                </ul>
            </div>
        </div>
    </nav>

    <main class="container-fluid p-4">
        <div class="row">
            <div class="col-md-12">
                <h2>🧪 M-Pesa Test Logs</h2>
                <hr>
            </div>
        </div>

        <div class="row">
            <div class="col-md-3">
                <div class="card">
                    <div class="card-header">Log Files</div>
                    <div class="card-body">
                        <?php if (empty($logFiles)): ?>
                            <p class="text-muted">No logs yet</p>
                        <?php else: ?>
                            <div class="list-group">
                                <?php foreach ($logFiles as $file): ?>
                                    <a href="?log=<?php echo urlencode($file); ?>" class="list-group-item list-group-item-action <?php echo $file === $selectedLog ? 'active' : ''; ?>">
                                        <?php echo $file; ?>
                                    </a>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <div class="col-md-9">
                <div class="card">
                    <div class="card-header d-flex justify-content-between">
                        <span><?php echo $selectedLog ?? 'No log selected'; ?></span>
                        <button class="btn btn-sm btn-danger" onclick="clearLogs()">Clear Logs</button>
                    </div>
                    <div class="card-body">
                        <pre style="background: #f8f9fa; padding: 15px; border-radius: 5px; max-height: 600px; overflow-y: auto; font-size: 12px;"><?php 
                        if ($logContent) {
                            echo htmlspecialchars($logContent);
                        } else {
                            echo "Select a log file to view";
                        }
                        ?></pre>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <script>
        function clearLogs() {
            if (confirm('Are you sure you want to delete all logs?')) {
                fetch('/Shinda/admin/clear_logs.php', { method: 'POST' })
                    .then(r => r.json())
                    .then(d => {
                        if (d.success) {
                            alert('Logs cleared');
                            location.reload();
                        }
                    });
            }
        }

        // Auto-refresh every 5 seconds
        setInterval(() => {
            location.reload();
        }, 5000);
    </script>
</body>
</html>