<?php

class GeoIPInfo
{
    private ?CMSAPI $api = null;

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

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

    /**
     * Register admin menu items.
     */
    public function getAdminMenu(): array
    {
        return [
            ['label' => 'GeoIP Info', 'route' => 'geoip-info', 'icon' => 'bi-globe2'],
        ];
    }

    /**
     * Register admin routes this plugin handles.
     */
    public function getAdminRoutes(): array
    {
        return ['geoip-info'];
    }

    /**
     * Handle admin route — render the admin page.
     */
    public function handleAdminRoute(string $route): void
    {
        $ip = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
        $geoip = new GeoIP();
        $countryCode = $geoip->lookupCountry($ip);
        $country = GeoIP::getCountryName($countryCode);
        $flag = GeoIP::getCountryFlagEmoji($countryCode);

        echo '<h2 class="mb-4"><i class="bi bi-globe2"></i> GeoIP Info</h2>';
        echo '<div class="card shadow-sm"><div class="card-body">';
        echo '<table class="table table-sm">';
        echo '<tr><td>IP adres</td><td><code>' . htmlspecialchars($ip) . '</code></td></tr>';
        echo '<tr><td>Land</td><td>' . $flag . ' ' . htmlspecialchars($country) . '</td></tr>';
        echo '</table>';
        echo '</div></div>';
    }
}