<?php
declare(strict_types=1);

require_once __DIR__ . '/api/config.php';
require_once __DIR__ . '/api/helpers.php';

header('Content-Type: application/xml; charset=utf-8');

$base = function_exists('appUrl') ? appUrl() : (
    ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http')
    . '://' . ($_SERVER['HTTP_HOST'] ?? 'localhost')
    . rtrim(dirname($_SERVER['SCRIPT_NAME'] ?? ''), '/\\')
);

$pages = [
    ['/', 'weekly', '1.0'],
    ['/index.php', 'weekly', '1.0'],
    ['/status.php', 'weekly', '0.7'],
    ['/account.php', 'monthly', '0.75'],
    ['/domain.php', 'weekly', '0.9'],
    ['/hosting.php', 'weekly', '0.9'],
    ['/dev-services.php', 'weekly', '0.85'],
    ['/email-marketing.php', 'weekly', '0.85'],
    ['/our-services.php', 'weekly', '0.85'],
    ['/domain-transfer.php', 'monthly', '0.8'],
    ['/hosting-order.php', 'monthly', '0.6'],
    ['/blog.php', 'weekly', '0.7'],
    ['/faq.php', 'monthly', '0.7'],
    ['/academy.php', 'weekly', '0.9'],
    ['/academy-courses.php', 'weekly', '0.9'],
    ['/academy-trainers.php', 'monthly', '0.7'],
    ['/academy-about.php', 'monthly', '0.6'],
    ['/academy-school.php', 'weekly', '0.85'],
    ['/academy-certificates.php', 'monthly', '0.6'],
    ['/iman/index.php', 'daily', '0.9'],
    ['/iman/video.php', 'daily', '0.85'],
    ['/iman/programs.php', 'weekly', '0.8'],
    ['/iman/archive.php', 'daily', '0.8'],
    ['/iman/books.php', 'weekly', '0.8'],
    ['/corporate.php', 'weekly', '0.9'],
    ['/corporate/programs.php', 'weekly', '0.8'],
    ['/corporate/approach.php', 'monthly', '0.7'],
    ['/corporate/contact.php', 'monthly', '0.8'],
    ['/corporate/programs.php', 'weekly', '0.85'],
    ['/corporate/approach.php', 'weekly', '0.8'],
    ['/corporate/contact.php', 'monthly', '0.7'],
    ['/privacy.php', 'yearly', '0.5'],
    ['/terms.php', 'yearly', '0.5'],
];

// كورسات الأكاديمية (روابط ديناميكية)
try {
    $pdoSitemap = function_exists('db') ? db() : null;
    if ($pdoSitemap instanceof PDO) {
        $slugs = $pdoSitemap->query('SELECT slug FROM academy_courses WHERE is_active = 1 ORDER BY sort_order ASC')->fetchAll(PDO::FETCH_COLUMN);
        foreach ($slugs as $slug) {
            $slug = trim((string) $slug);
            if ($slug !== '') {
                $pages[] = ['/academy-course.php?slug=' . rawurlencode($slug), 'weekly', '0.8'];
            }
        }
    }
} catch (Throwable) {
    // non-blocking
}

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($pages as [$path, $freq, $priority]): ?>
  <url>
    <loc><?= htmlspecialchars($base . $path) ?></loc>
    <changefreq><?= $freq ?></changefreq>
    <priority><?= $priority ?></priority>
  </url>
<?php endforeach; ?>
</urlset>
