<?php

class Logs
{
    private ?PluginAPIInterface $api = null;

    public function setAPI(PluginAPIInterface $api): void
    {
        $this->api = $api;
    }

    public function getConfig(): array
    {
        return [
            'title' => 'Logs',
            'viewable' => false,
            'type' => 'system',
        ];
    }

    public function getAdminMenu(): array
    {
        return [
            [
                'plugin' => 'Logs',
                'route' => 'logs',
                'label' => 'Logs',
                'icon' => 'bi-journal-text',
                'section' => 'general',
            ],
        ];
    }

    public function handleAdminRoute(string $action): ?string
    {
        $tab = $_GET['tab'] ?? 'admin';
        $logDir = dirname(__DIR__, 2) . '/admin/storage/logs';
        $logFile = $tab === 'requests' ? $logDir . '/requests.log' : $logDir . '/admin.log';

        $logs = [];
        if (file_exists($logFile)) {
            $lines = file($logFile) ?: [];
            $lines = array_slice($lines, -100);
            foreach ($lines as $line) {
                if (preg_match('/^\[([^\]]+)\] \[([^\]]+)\] \[([^\]]+)\] (.+)$/', trim($line), $m)) {
                    $logs[] = [
                        'time' => $m[1],
                        'level' => strtolower($m[2]),
                        'ip' => $m[3],
                        'message' => $m[4],
                    ];
                }
            }
            $logs = array_reverse($logs);
        }

        ob_start();
        ?>
        <h2 class="mb-4"><i class="bi bi-journal-text"></i> Logs</h2>

        <ul class="nav nav-tabs mb-3">
            <li class="nav-item">
                <a class="nav-link <?= $tab === 'admin' ? 'active' : '' ?>" href="/admin/logs?tab=admin">Admin</a>
            </li>
            <li class="nav-item">
                <a class="nav-link <?= $tab === 'requests' ? 'active' : '' ?>" href="/admin/logs?tab=requests">Requests</a>
            </li>
        </ul>

        <div class="card shadow-sm">
            <div class="card-body p-0">
                <table class="table table-hover mb-0">
                    <thead>
                        <tr>
                            <th>Tijd</th>
                            <th>Level</th>
                            <th>IP</th>
                            <th>Bericht</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($logs)): ?>
                        <tr><td colspan="4" class="text-muted text-center py-4">Geen logs gevonden.</td></tr>
                        <?php else: ?>
                            <?php foreach ($logs as $log): ?>
                            <tr>
                                <td class="text-muted small"><?= htmlspecialchars($log['time']) ?></td>
                                <td><span class="badge bg-<?= $log['level'] === 'warning' ? 'warning text-dark' : ($log['level'] === 'error' ? 'danger' : 'info') ?>"><?= htmlspecialchars($log['level']) ?></span></td>
                                <td><code class="text-muted"><?= htmlspecialchars($log['ip']) ?></code></td>
                                <td><?= htmlspecialchars($log['message']) ?></td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }
}