<?php

/*
|--------------------------------------------------------------------------
| VITE MANIFEST
|--------------------------------------------------------------------------
*/

$manifestPath = __DIR__ . '/.vite/manifest.json';

$manifest = json_decode(file_get_contents($manifestPath), true);

$entry = $manifest['index.html'];

$jsFile = $entry['file'];
$cssFiles = $entry['css'] ?? [];

/*
|--------------------------------------------------------------------------
| PEGAR cod_noticia (MELHOR QUE URL PATH)
|--------------------------------------------------------------------------
*/

$newsId = $_GET['cod_noticia'] ?? null;

/*
|--------------------------------------------------------------------------
| DADOS PADRÃO
|--------------------------------------------------------------------------
*/

$title = 'Portal Municipal';
$description = 'Confira as últimas notícias do portal.';
$image = 'https://bc.sc.gov.br/assets/prefeituralogo-COK2MikU.png';

/*
|--------------------------------------------------------------------------
| BUSCAR NA API
|--------------------------------------------------------------------------
*/

if (!empty($newsId)) {

    $apiUrl = "https://sim.bc.sc.gov.br/noticia/" . urlencode($newsId);

    $response = @file_get_contents($apiUrl);

    if ($response) {

        $data = json_decode($response, true);

        if (is_array($data)) {
            $title = $data['title'] ?? $title;
            $description = $data['descricao'] ?? $description;
            $image = $data['photos'][0] ?? $image;
        }
    }
}



$url = (
    isset($_SERVER['HTTPS']) ? 'https://' : 'http://'
) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];


$title = htmlspecialchars($title);
$description = htmlspecialchars($description);
$image = htmlspecialchars($image);
$url = htmlspecialchars($url);
?>
<!doctype html>
<html lang="pt-br">

<head>
    <meta charset="UTF-8" />

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    />

    <title><?= $title ?></title>

    <!-- SEO -->
    <meta
        name="description"
        content="<?= $description ?>"
    />

    <!-- OPEN GRAPH -->
    <meta property="og:type" content="article" />

    <meta
        property="og:title"
        content="<?= $title ?>"
    />

    <meta
        property="og:description"
        content="<?= $description ?>"
    />

    <meta
        property="og:image"
        content="<?= $image ?>"
    />

    <meta
        property="og:url"
        content="<?= $url ?>"
    />

    <meta
        property="og:site_name"
        content="Portal Municipal"
    />

    <meta property="og:locale" content="pt_BR" />

    <!-- TWITTER -->
    <meta
        name="twitter:card"
        content="summary_large_image"
    />

    <meta
        name="twitter:title"
        content="<?= $title ?>"
    />

    <meta
        name="twitter:description"
        content="<?= $description ?>"
    />

    <meta
        name="twitter:image"
        content="<?= $image ?>"
    />

    <!-- CSS VITE -->
    <?php foreach ($cssFiles as $css): ?>
        <link
            rel="stylesheet"
            href="/<?= $css ?>"
        >
    <?php endforeach; ?>

    <!-- JS VITE -->
    <script
        type="module"
        src="/<?= $jsFile ?>"
    ></script>
</head>

<body>
    <div id="root"></div>
</body>

</html>
