<?php
// ============================================
// CareerFlora — sitemap.php [FULL WORKING VERSION]
// Generates sitemap.xml fresh from the database on every request.
// Uses ONLY clean canonical URLs (/post/slug, /category/slug,
// /listing.php?sub=slug — matching what includes/header.php actually
// links to). Never .php-with-slug, never ?slug= duplicates.
//
// SAFE BY DESIGN: if the database or a query fails for any reason,
// this script still outputs a VALID (if partial) XML sitemap instead
// of crashing or outputting raw PHP errors into the XML — which would
// break Search Console validation.
// ============================================

require_once __DIR__ . '/includes/db.php'; // adjust path if db.php lives elsewhere

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

// Suppress on-screen error/warning output so nothing can leak into the
// XML body and corrupt it. Errors still get logged normally by PHP.
error_reporting(E_ALL);
ini_set('display_errors', '0');

$urls = [];

// ── Homepage ──────────────────────────────────────────────────────────────
$urls[] = [
    'loc'        => rtrim(SITE_URL, '/') . '/',
    'lastmod'    => date('Y-m-d'),
    'changefreq' => 'daily',
    'priority'   => '1.0',
];

// ── All published posts — CLEAN URL ONLY ────────────────────────────────
try {
    $stmt = $pdo->query("SELECT slug, created_at FROM posts WHERE status = 'published' ORDER BY created_at DESC");
    while ($post = $stmt->fetch()) {
        $slug = trim((string)($post['slug'] ?? ''));
        if ($slug === '') continue; // skip broken/empty slugs, prevents malformed <loc>

        $urls[] = [
            'loc'        => rtrim(SITE_URL, '/') . '/post/' . rawurlencode($slug),
            'lastmod'    => !empty($post['created_at']) ? date('Y-m-d', strtotime($post['created_at'])) : date('Y-m-d'),
            'changefreq' => 'weekly',
            'priority'   => '0.8',
        ];
    }
} catch (Throwable $e) {
    // Table/column mismatch or DB hiccup — skip posts, don't break the file
    error_log('sitemap.php posts query failed: ' . $e->getMessage());
}

// ── All active categories — CLEAN URL ONLY ──────────────────────────────
try {
    $stmt = $pdo->query("SELECT slug FROM categories WHERE is_active = 1");
    while ($cat = $stmt->fetch()) {
        $slug = trim((string)($cat['slug'] ?? ''));
        if ($slug === '') continue;

        // Two categories (study-resources, blog) map to their own static
        // pages elsewhere in the codebase (see catUrl() in header.php) —
        // mirror that here so the sitemap never lists a URL that
        // immediately redirects elsewhere.
        $loc = $slug === 'study-resources'
            ? rtrim(SITE_URL, '/') . '/study-materials.php'
            : rtrim(SITE_URL, '/') . '/category/' . rawurlencode($slug);

        $urls[] = [
            'loc'        => $loc,
            'lastmod'    => date('Y-m-d'),
            'changefreq' => 'daily',
            'priority'   => '0.7',
        ];
    }
} catch (Throwable $e) {
    error_log('sitemap.php categories query failed: ' . $e->getMessage());
}

// ── All active sub-categories — matches includes/header.php's real link
//    pattern (/listing.php?sub=slug), not the old /sub-category.php?slug=
//    pattern the previous static sitemap used ────────────────────────────
try {
    $stmt = $pdo->query("SELECT slug FROM sub_categories WHERE is_active = 1");
    while ($sub = $stmt->fetch()) {
        $slug = trim((string)($sub['slug'] ?? ''));
        if ($slug === '') continue;

        $urls[] = [
            'loc'        => rtrim(SITE_URL, '/') . '/listing.php?sub=' . rawurlencode($slug),
            'lastmod'    => date('Y-m-d'),
            'changefreq' => 'weekly',
            'priority'   => '0.6',
        ];
    }
} catch (Throwable $e) {
    error_log('sitemap.php sub_categories query failed: ' . $e->getMessage());
}

// ── Output valid XML no matter what happened above ──────────────────────
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

foreach ($urls as $u) {
    echo "  <url>\n";
    echo "    <loc>"        . htmlspecialchars($u['loc'], ENT_QUOTES, 'UTF-8') . "</loc>\n";
    echo "    <lastmod>"    . htmlspecialchars($u['lastmod'], ENT_QUOTES, 'UTF-8') . "</lastmod>\n";
    echo "    <changefreq>" . htmlspecialchars($u['changefreq'], ENT_QUOTES, 'UTF-8') . "</changefreq>\n";
    echo "    <priority>"   . htmlspecialchars($u['priority'], ENT_QUOTES, 'UTF-8') . "</priority>\n";
    echo "  </url>\n";
}

echo '</urlset>' . "\n";