<?php
header('Content-Type: application/xml; charset=UTF-8');

$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
$host = preg_replace('/[^a-zA-Z0-9.\-\[\]:]/', '', $host);
$scriptDir = str_replace('\\', '/', dirname(isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : '/sitemap.xml'));
if ($scriptDir === '.' || $scriptDir === '\\') {
    $scriptDir = '';
}
$baseUrl = rtrim($scheme . '://' . $host . ($scriptDir === '/' ? '' : $scriptDir), '/');

$pages = [
    ['index.php', 'daily', '1.0'],
    ['about.php', 'monthly', '0.8'],
    ['services.php', 'weekly', '0.9'],
    ['gallery.php', 'monthly', '0.7'],
    ['video-gallery.php', 'monthly', '0.7'],
    ['blog.php', 'weekly', '0.8'],
    ['appointment.php', 'monthly', '0.8'],
    ['contact.php', 'monthly', '0.8'],
    ['privacy-policy.php', 'yearly', '0.3'],
    ['terms-conditions.php', 'yearly', '0.3'],
];

$entries = [];

foreach ($pages as $page) {
    $entries[] = [
        'loc' => $baseUrl . '/' . $page[0],
        'changefreq' => $page[1],
        'priority' => $page[2],
    ];
}

include './control-panel/include/connection.php';

$dynamicPages = [
    'service' => ['table' => 'service', 'file' => 'service-details.php', 'parameter' => 'id', 'changefreq' => 'monthly', 'priority' => '0.8'],
    'facility' => ['table' => 'facility', 'file' => 'facility-details.php', 'parameter' => 'id', 'changefreq' => 'monthly', 'priority' => '0.7'],
    'clinic' => ['table' => 'clinic', 'file' => 'clinic-details.php', 'parameter' => 'clinic', 'changefreq' => 'monthly', 'priority' => '0.7'],
    'blog' => ['table' => 'blog', 'file' => 'blog-details.php', 'parameter' => 'bid', 'changefreq' => 'weekly', 'priority' => '0.8'],
];

foreach ($dynamicPages as $dynamic) {
    $table = str_replace('`', '', $dynamic['table']);
    $result = $conn->query("SELECT id FROM `" . $table . "`");

    if ($result) {
        while ($row = $result->fetch_assoc()) {
            $entries[] = [
                'loc' => $baseUrl . '/' . $dynamic['file'] . '?' . rawurlencode($dynamic['parameter']) . '=' . urlencode((string) $row['id']),
                'changefreq' => $dynamic['changefreq'],
                'priority' => $dynamic['priority'],
            ];
        }
        $result->free();
    }
}

$conn->close();

$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;
$urlset = $xml->appendChild($xml->createElement('urlset'));
$urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');

foreach ($entries as $entry) {
    $url = $xml->createElement('url');
    $urlset->appendChild($url);
    $url->appendChild($xml->createElement('loc', $entry['loc']));
    $url->appendChild($xml->createElement('changefreq', $entry['changefreq']));
    $url->appendChild($xml->createElement('priority', $entry['priority']));
}

echo $xml->saveXML();