<?php

declare(strict_types=1);


/*
|--------------------------------------------------------------------------
| XML HEADERS
|--------------------------------------------------------------------------
*/

header('Content-Type: application/xml; charset=UTF-8');
header('X-Content-Type-Options: nosniff');


/*
|--------------------------------------------------------------------------
| BOOTSTRAP
|--------------------------------------------------------------------------
*/

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


/*
|--------------------------------------------------------------------------
| HELPERS
|--------------------------------------------------------------------------
*/

function sitemap_xml_escape(string $value): string
{
    return htmlspecialchars(
        $value,
        ENT_XML1 | ENT_QUOTES,
        'UTF-8'
    );
}


function sitemap_url(
    string $loc,
    ?string $lastmod = null,
    array $alternates = []
): string {

    $xml = "  <url>\n";

    $xml .=
        "    <loc>" .
        sitemap_xml_escape($loc) .
        "</loc>\n";


    if ($lastmod !== null) {

        $xml .=
            "    <lastmod>" .
            sitemap_xml_escape($lastmod) .
            "</lastmod>\n";
    }


    foreach ($alternates as $lang => $href) {

        $xml .=
            '    <xhtml:link rel="alternate" hreflang="' .
            sitemap_xml_escape((string)$lang) .
            '" href="' .
            sitemap_xml_escape((string)$href) .
            "\" />\n";
    }


    $xml .= "  </url>\n";

    return $xml;
}


/*
|--------------------------------------------------------------------------
| CURRENT HOST
|--------------------------------------------------------------------------
*/

$host = strtolower(
    preg_replace(
        '/:\d+$/',
        '',
        $_SERVER['HTTP_HOST'] ?? ''
    )
);


$host = preg_replace(
    '/^www\./',
    '',
    $host
);


/*
|--------------------------------------------------------------------------
| MARKETPLACE HOSTS
|--------------------------------------------------------------------------
*/

$marketplaceHosts =
    $config['app']['marketplace_hosts']
    ?? [
        'namevora.io',
        'www.namevora.io',
        'namevora.net',
        'www.namevora.net',
        'namevora.si',
        'www.namevora.si'
    ];


$normalizedMarketplaceHosts = [];

foreach ($marketplaceHosts as $marketHost) {

    $marketHost = strtolower(
        preg_replace(
            '/:\d+$/',
            '',
            (string)$marketHost
        )
    );

    $marketHost = preg_replace(
        '/^www\./',
        '',
        $marketHost
    );

    $normalizedMarketplaceHosts[] =
        $marketHost;
}


/*
|--------------------------------------------------------------------------
| XML START
|--------------------------------------------------------------------------
*/

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";

echo
    '<urlset ' .
    'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' .
    'xmlns:xhtml="http://www.w3.org/1999/xhtml">' .
    "\n";


/*
|--------------------------------------------------------------------------
| NAMEVORA MAIN MARKETPLACE
|--------------------------------------------------------------------------
*/

if (
    in_array(
        $host,
        $normalizedMarketplaceHosts,
        true
    )
) {

    $base =
        'https://namevora.io/';


    echo sitemap_url(
        $base,
        null,
        [
            'en' =>
                $base,

            'sl' =>
                $base . '?lang=sl',

            'de' =>
                $base . '?lang=de',

            'x-default' =>
                $base
        ]
    );


    echo sitemap_url(
        $base . '?lang=sl'
    );


    echo sitemap_url(
        $base . '?lang=de'
    );
}


/*
|--------------------------------------------------------------------------
| DOMAIN LANDER
|--------------------------------------------------------------------------
*/

else {

    $stmt =
        $pdo->prepare(
            'SELECT
                id,
                domain,
                status,
                marketplace_visible,
                lander_enabled,
                updated_at
             FROM domains
             WHERE domain = ?
             LIMIT 1'
        );


    $stmt->execute([
        $host
    ]);


    $domain =
        $stmt->fetch();


    if (
        $domain
        && strtoupper(
            (string)$domain['status']
        ) === 'FOR_SALE'
        && (int)$domain[
            'marketplace_visible'
        ] === 1
        && (int)$domain[
            'lander_enabled'
        ] === 1
    ) {

        $base =
            'https://' .
            $host .
            '/';


        $lastmod = null;


        if (
            !empty(
                $domain['updated_at']
            )
        ) {

            $timestamp =
                strtotime(
                    (string)$domain[
                        'updated_at'
                    ]
                );


            if ($timestamp !== false) {

                $lastmod =
                    date(
                        'Y-m-d',
                        $timestamp
                    );
            }
        }


        echo sitemap_url(
            $base,
            $lastmod,
            [
                'en' =>
                    $base,

                'sl' =>
                    $base . '?lang=sl',

                'de' =>
                    $base . '?lang=de',

                'x-default' =>
                    $base
            ]
        );


        echo sitemap_url(
            $base . '?lang=sl',
            $lastmod
        );


        echo sitemap_url(
            $base . '?lang=de',
            $lastmod
        );
    }
}


/*
|--------------------------------------------------------------------------
| XML END
|--------------------------------------------------------------------------
*/

echo "</urlset>\n";

exit;