<?php
declare(strict_types=1);

require_once __DIR__ . '/bootstrap.php';
require_once __DIR__ . '/admin_auth_lib.php';

mocker_admin_require_any_permission(['sponsors', 'bonuses', 'social', 'site', 'market']);

$dataDir = dirname(__DIR__) . '/data/';
$read = static function (string $file): array {
    $path = $file;
    if (!is_readable($path)) {
        return [];
    }
    $raw = (string) file_get_contents($path);
    if (str_starts_with($raw, "\xEF\xBB\xBF")) {
        $raw = substr($raw, 3);
    }
    $j = json_decode($raw, true);
    return is_array($j) ? $j : [];
};

$payload = ['ok' => true];
if (mocker_admin_can('sponsors')) {
    $payload['sponsors'] = $read($dataDir . 'sponsors.json');
    $payload['sponsorPromoCodes'] = $read($dataDir . 'sponsor_promo_codes.json');
} else {
    $payload['sponsors'] = [];
    $payload['sponsorPromoCodes'] = [];
}
if (mocker_admin_can('bonuses') || mocker_admin_can('sponsors')) {
    $payload['bonuses'] = $read($dataDir . 'bonuses.json');
} else {
    $payload['bonuses'] = [];
}
if (mocker_admin_can('social')) {
    $payload['social'] = $read($dataDir . 'social.json');
} else {
    $payload['social'] = [];
}
if (mocker_admin_can('site')) {
    $payload['site'] = $read($dataDir . 'site.json');
    $payload['popup'] = $read($dataDir . 'popup.json');
    $payload['gamdomTurnuva'] = $read($dataDir . 'gamdom-turnuva.json');
    $legalRaw = $read($dataDir . 'legal-pages.json');
    $payload['legalPages'] = is_array($legalRaw) && $legalRaw !== [] ? $legalRaw : new stdClass();
} else {
    $payload['site'] = [];
    $payload['popup'] = [];
    $payload['gamdomTurnuva'] = [];
    $payload['legalPages'] = new stdClass();
}
if (mocker_admin_can('market')) {
    $payload['market'] = $read($dataDir . 'market.json');
} else {
    $payload['market'] = [];
}

echo json_encode(
    $payload,
    JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
);
