<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache, must-revalidate');

ini_set('display_errors', 0);
error_reporting(0);

$result = [];

// ===== EVENTS (logs parsés par mc-log-watcher) =====
$logsFile = '/var/cache/mc-logs.json';
if (file_exists($logsFile)) {
    $raw = @file_get_contents($logsFile);
    $decoded = $raw ? @json_decode($raw, true) : null;
    $result['events'] = $decoded['events'] ?? [];
    // Renvoyer les N derniers triés du plus récent au plus ancien
    $result['events'] = array_reverse(array_slice($result['events'], -100));
} else {
    $result['events'] = [];
}

// ===== WHITELIST =====
$whitelists = [
    'mc1' => '/srv/Minecraft/whitelist.json',
    'mc2' => '/srv/Fabric-1.21.8/whitelist.json',
];
$result['whitelist'] = [];
foreach ($whitelists as $key => $path) {
    if (file_exists($path)) {
        $raw = @file_get_contents($path);
        $players = $raw ? @json_decode($raw, true) : [];
        $result['whitelist'][$key] = is_array($players) ? $players : [];
    } else {
        $result['whitelist'][$key] = null; // null = whitelist désactivée / fichier absent
    }
}

// ===== BANNISSEMENTS =====
$banlists = [
    'mc1' => '/srv/Minecraft/banned-players.json',
    'mc2' => '/srv/Fabric-1.21.8/banned-players.json',
];
$result['banned'] = [];
foreach ($banlists as $key => $path) {
    if (file_exists($path)) {
        $raw = @file_get_contents($path);
        $players = $raw ? @json_decode($raw, true) : [];
        $result['banned'][$key] = is_array($players) ? $players : [];
    } else {
        $result['banned'][$key] = [];
    }
}

// ===== MODS (Fabric uniquement - dossier mods/) =====
$modsDir = '/srv/Fabric-1.21.8/mods';
$result['mods'] = [];
if (is_dir($modsDir)) {
    $files = @scandir($modsDir);
    if ($files) {
        foreach ($files as $file) {
            if (pathinfo($file, PATHINFO_EXTENSION) === 'jar') {
                // Tenter de lire fabric.mod.json à l'intérieur du jar (zip)
                $jarPath = $modsDir . '/' . $file;
                $modInfo = ['filename' => $file, 'name' => null, 'version' => null, 'description' => null];

                // Lire le fabric.mod.json depuis le jar (zip)
                if (class_exists('ZipArchive')) {
                    $zip = new ZipArchive();
                    if ($zip->open($jarPath) === true) {
                        $idx = $zip->locateName('fabric.mod.json');
                        if ($idx !== false) {
                            $json = @json_decode($zip->getFromIndex($idx), true);
                            if ($json) {
                                $modInfo['name']        = $json['name']        ?? $json['id']  ?? null;
                                $modInfo['version']     = $json['version']     ?? null;
                                $modInfo['description'] = $json['description'] ?? null;
                                $modInfo['id']          = $json['id']          ?? null;
                            }
                        }
                        $zip->close();
                    }
                }

                // Fallback: nom depuis le fichier
                if (!$modInfo['name']) {
                    $modInfo['name'] = preg_replace('/[-_][\d.]+.*\.jar$/', '', $file);
                }

                $result['mods'][] = $modInfo;
            }
        }
        // Trier par nom
        usort($result['mods'], fn($a, $b) => strcasecmp($a['name'] ?? '', $b['name'] ?? ''));
    }
}

// ===== SERVER PROPERTIES (whitelist activée ?) =====
$propsFiles = [
    'mc1' => '/srv/Minecraft/server.properties',
    'mc2' => '/srv/Fabric-1.21.8/server.properties',
];
$result['whitelist_enabled'] = [];
foreach ($propsFiles as $key => $path) {
    $result['whitelist_enabled'][$key] = false;
    if (file_exists($path)) {
        $content = @file_get_contents($path);
        if ($content && preg_match('/^white-list\s*=\s*true/m', $content)) {
            $result['whitelist_enabled'][$key] = true;
        }
    }
}

echo json_encode(['success' => true, 'data' => $result], JSON_UNESCAPED_UNICODE);
?>
