<?php
// Sitemap generator - Auto detects all posts and tools
header('Content-Type: application/xml');
header('X-Robots-Tag: noindex, follow');

require_once 'config/database.php';
$pdo = getDB();

$baseUrl = rtrim(BASE_URL, '/');
$currentDate = date('Y-m-d');

// Clean output buffer
ob_clean();

// Start XML - Proper format without extra spaces
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

// 1. Homepage
echo '  <url>' . "\n";
echo '    <loc>' . $baseUrl . '/</loc>' . "\n";
echo '    <lastmod>' . $currentDate . '</lastmod>' . "\n";
echo '    <changefreq>daily</changefreq>' . "\n";
echo '    <priority>1.0</priority>' . "\n";
echo '  </url>' . "\n";

// 2. All Tools Page
echo '  <url>' . "\n";
echo '    <loc>' . $baseUrl . '/tools/</loc>' . "\n";
echo '    <lastmod>' . $currentDate . '</lastmod>' . "\n";
echo '    <changefreq>weekly</changefreq>' . "\n";
echo '    <priority>0.9</priority>' . "\n";
echo '  </url>' . "\n";

// 3. Blog Page
echo '  <url>' . "\n";
echo '    <loc>' . $baseUrl . '/blog/</loc>' . "\n";
echo '    <lastmod>' . $currentDate . '</lastmod>' . "\n";
echo '    <changefreq>daily</changefreq>' . "\n";
echo '    <priority>0.8</priority>' . "\n";
echo '  </url>' . "\n";

// 4. Get all published posts
try {
    $stmt = $pdo->prepare("SELECT id, title, slug, custom_url, updated_at FROM posts WHERE status = 'published' ORDER BY created_at DESC");
    $stmt->execute();
    $posts = $stmt->fetchAll();
    
    foreach ($posts as $post) {
        $postUrl = !empty($post['custom_url']) ? $post['custom_url'] : $post['slug'];
        $lastmod = !empty($post['updated_at']) ? date('Y-m-d', strtotime($post['updated_at'])) : $currentDate;
        
        echo '  <url>' . "\n";
        echo '    <loc>' . $baseUrl . '/blog/' . htmlspecialchars($postUrl) . '</loc>' . "\n";
        echo '    <lastmod>' . $lastmod . '</lastmod>' . "\n";
        echo '    <changefreq>weekly</changefreq>' . "\n";
        echo '    <priority>0.7</priority>' . "\n";
        echo '  </url>' . "\n";
    }
} catch (PDOException $e) {
    // Table might not exist yet - silent fail
}

// 5. Get all active tools
try {
    $stmt = $pdo->prepare("SELECT id, name, slug, updated_at FROM tools WHERE status = 'active' ORDER BY id DESC");
    $stmt->execute();
    $tools = $stmt->fetchAll();
    
    foreach ($tools as $tool) {
        $lastmod = !empty($tool['updated_at']) ? date('Y-m-d', strtotime($tool['updated_at'])) : $currentDate;
        
        echo '  <url>' . "\n";
        echo '    <loc>' . $baseUrl . '/tools/' . htmlspecialchars($tool['slug']) . '.php</loc>' . "\n";
        echo '    <lastmod>' . $lastmod . '</lastmod>' . "\n";
        echo '    <changefreq>monthly</changefreq>' . "\n";
        echo '    <priority>0.8</priority>' . "\n";
        echo '  </url>' . "\n";
    }
} catch (PDOException $e) {
    // Table might not exist yet - silent fail
}

// 6. Static Pages
$staticPages = [
    'about' => 'About Us',
    'contact' => 'Contact Us',
    'privacy-policy' => 'Privacy Policy',
    'terms' => 'Terms & Conditions'
];

foreach ($staticPages as $slug => $title) {
    echo '  <url>' . "\n";
    echo '    <loc>' . $baseUrl . '/pages/' . $slug . '.php</loc>' . "\n";
    echo '    <lastmod>' . $currentDate . '</lastmod>' . "\n";
    echo '    <changefreq>monthly</changefreq>' . "\n";
    echo '    <priority>0.5</priority>' . "\n";
    echo '  </url>' . "\n";
}

// 7. Categories
try {
    $stmt = $pdo->prepare("SELECT id, name, slug FROM categories ORDER BY id DESC");
    $stmt->execute();
    $categories = $stmt->fetchAll();
    
    foreach ($categories as $category) {
        echo '  <url>' . "\n";
        echo '    <loc>' . $baseUrl . '/category/' . htmlspecialchars($category['slug']) . '</loc>' . "\n";
        echo '    <lastmod>' . $currentDate . '</lastmod>' . "\n";
        echo '    <changefreq>weekly</changefreq>' . "\n";
        echo '    <priority>0.6</priority>' . "\n";
        echo '  </url>' . "\n";
    }
} catch (PDOException $e) {
    // Table might not exist yet - silent fail
}

// Close XML
echo '</urlset>';

// No extra spaces or characters after this
exit;
?>