<?php
header('Content-Type: application/manifest+json');

require_once __DIR__ . '/g55/services/database.php';

$nome    = '29uu9';
$favicon = '';
$logo    = '';

$result = $mysqli->query("SELECT nome, favicon, logo FROM config LIMIT 1");
if ($result && $row = $result->fetch_assoc()) {
    $nome    = $row['nome']    ?: '29uu9';
    $favicon = $row['favicon'] ?: '';
    $logo    = $row['logo']    ?: '';
}

$base = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];

function resolveUrl($path, $base) {
    if (empty($path)) return $base . '/assets/favicon.png';
    if (strpos($path, 'http') === 0) return $path;
    if (strpos($path, '/') !== 0) $path = '/uploads/' . $path;
    return $base . $path;
}

$iconUrl = resolveUrl($favicon ?: $logo, $base);

$manifest = [
    "name"             => $nome,
    "short_name"       => $nome,
    "description"      => "Jogue os melhores jogos em " . $nome,
    "start_url"        => "/",
    "scope"            => "/",
    "display"          => "standalone",
    "orientation"      => "portrait",
    "background_color" => "#090F1F",
    "theme_color"      => "#090F1F",
    "lang"             => "pt-BR",
    "icons"            => [
        ["src" => $iconUrl, "sizes" => "192x192", "type" => "image/png", "purpose" => "any maskable"],
        ["src" => $iconUrl, "sizes" => "512x512", "type" => "image/png", "purpose" => "any maskable"],
    ],
    "categories" => ["games", "entertainment"],
];

echo json_encode($manifest, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
