<?php

class Statistics
{
    private ?PluginAPIInterface $api = null;

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

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

    public function getAdminMenu(): array
    {
        return [
            [
                'plugin' => 'Statistics',
                'route' => 'statistics',
                'label' => 'Statistieken',
                'icon' => 'bi-bar-chart',
                'section' => 'general',
            ],
        ];
    }

    public function handleAdminRoute(string $action): ?string
    {
        $analytics = $this->getAnalytics();
        if ($analytics === null) {
            return '<div class="alert alert-warning">Analytics is uitgeschakeld.</div>';
        }

        $stats = $analytics->getStats();
        $totals = $stats['totals'] ?? ['views' => 0, 'uniques' => 0, 'pages' => []];
        $countries = $stats['countries'] ?? [];
        $topPages = $stats['top_pages'] ?? $totals['pages'] ?? [];

        ob_start();
        ?>
        <h2 class="mb-4"><i class="bi bi-bar-chart"></i> Statistieken</h2>

        <div class="row g-4 mb-4">
            <div class="col-md-4">
                <div class="card stat-card shadow-sm">
                    <div class="card-body d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">Totaal aantal views</h6>
                            <h3 class="mb-0"><?= number_format($totals['views'] ?? 0, 0, ',', '.') ?></h3>
                        </div>
                        <i class="bi bi-eye stat-icon text-primary"></i>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card stat-card shadow-sm">
                    <div class="card-body d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">Unieke bezoekers</h6>
                            <h3 class="mb-0"><?= number_format($totals['uniques'] ?? 0, 0, ',', '.') ?></h3>
                        </div>
                        <i class="bi bi-people stat-icon text-success"></i>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card stat-card shadow-sm">
                    <div class="card-body d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">Pagina views</h6>
                            <h3 class="mb-0"><?= number_format($totals['page_views'] ?? ($totals['views'] ?? 0), 0, ',', '.') ?></h3>
                        </div>
                        <i class="bi bi-file-earmark-text stat-icon text-info"></i>
                    </div>
                </div>
            </div>
        </div>

        <div class="row g-4">
            <div class="col-md-6">
                <div class="card shadow-sm">
                    <div class="card-header"><i class="bi bi-globe"></i> Landen</div>
                    <div class="card-body">
                        <?php if (empty($countries)): ?>
                            <p class="text-muted mb-0">Geen data</p>
                        <?php else: ?>
                            <table class="table table-sm mb-0">
                                <thead><tr><th>Land</th><th>Views</th></tr></thead>
                                <tbody>
                                <?php foreach ($countries as $country => $count): ?>
                                    <tr>
                                        <td><?= GeoIP::getCountryFlagEmoji($country) ?> <?= htmlspecialchars(GeoIP::getCountryName($country)) ?></td>
                                        <td><?= number_format($count, 0, ',', '.') ?></td>
                                    </tr>
                                <?php endforeach; ?>
                                </tbody>
                            </table>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card shadow-sm">
                    <div class="card-header"><i class="bi bi-file-earmark-bar"></i> Top pagina's</div>
                    <div class="card-body">
                        <?php if (empty($topPages)): ?>
                            <p class="text-muted mb-0">Geen data</p>
                        <?php else: ?>
                            <table class="table table-sm mb-0">
                                <thead><tr><th>Pagina</th><th>Views</th></tr></thead>
                                <tbody>
                                <?php foreach ($topPages as $page => $count): ?>
                                    <tr>
                                        <td><?= htmlspecialchars($page) ?></td>
                                        <td><?= number_format($count, 0, ',', '.') ?></td>
                                    </tr>
                                <?php endforeach; ?>
                                </tbody>
                            </table>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }

    private function getAnalytics(): ?Analytics
    {
        if ($this->api === null) {
            return null;
        }
        $analyticsConfig = $this->api->getConfig('analytics', []);
        if (!is_array($analyticsConfig) || empty($analyticsConfig['enabled'])) {
            return null;
        }
        return new Analytics($analyticsConfig);
    }
}