<?php
require_once __DIR__ . '/config/db.php';
require_once __DIR__ . '/config/settings.php';

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

$staticPages = [
    ['loc' => '', 'priority' => '1.0', 'changefreq' => 'daily'],
    ['loc' => 'login.php', 'priority' => '0.5', 'changefreq' => 'monthly'],
    ['loc' => 'register.php', 'priority' => '0.5', 'changefreq' => 'monthly'],
    ['loc' => 'chat.php', 'priority' => '0.9', 'changefreq' => 'daily'],
    ['loc' => 'blog.php', 'priority' => '0.8', 'changefreq' => 'daily'],
    ['loc' => 'about.php', 'priority' => '0.5', 'changefreq' => 'monthly'],
    ['loc' => 'pricing.php', 'priority' => '0.6', 'changefreq' => 'weekly'],
    ['loc' => 'prime.php', 'priority' => '0.6', 'changefreq' => 'weekly'],
    ['loc' => 'developer.php', 'priority' => '0.5', 'changefreq' => 'weekly'],
    ['loc' => 'developer-docs.php', 'priority' => '0.5', 'changefreq' => 'weekly'],
    ['loc' => 'app.php', 'priority' => '0.7', 'changefreq' => 'weekly'],
    ['loc' => 'download.php', 'priority' => '0.5', 'changefreq' => 'monthly'],
    ['loc' => 'terms.php', 'priority' => '0.3', 'changefreq' => 'yearly'],
    ['loc' => 'privacy-policy.php', 'priority' => '0.3', 'changefreq' => 'yearly'],
    ['loc' => 'disclaimer.php', 'priority' => '0.3', 'changefreq' => 'yearly'],
    ['loc' => 'refund-policy.php', 'priority' => '0.3', 'changefreq' => 'yearly'],
    ['loc' => 'delete-account.php', 'priority' => '0.2', 'changefreq' => 'yearly'],
];

$articles = [];
try {
    $pdo->exec("CREATE TABLE IF NOT EXISTS articles (
        id INT AUTO_INCREMENT PRIMARY KEY,
        title VARCHAR(255) NOT NULL,
        slug VARCHAR(255) NOT NULL UNIQUE,
        content LONGTEXT NOT NULL,
        excerpt TEXT,
        featured_image VARCHAR(500),
        category VARCHAR(100) DEFAULT 'General',
        tags VARCHAR(500),
        author_id INT NOT NULL,
        status ENUM('draft','published') DEFAULT 'published',
        published_at TIMESTAMP NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    )");
    $stmt = $pdo->query("SELECT slug, updated_at, published_at FROM articles WHERE status = 'published' ORDER BY published_at DESC");
    $articles = $stmt->fetchAll();
} catch (Exception $e) {}

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<?php foreach ($staticPages as $page): ?>
    <url>
        <loc><?php echo SITE_URL . '/' . $page['loc']; ?></loc>
        <priority><?php echo $page['priority']; ?></priority>
        <changefreq><?php echo $page['changefreq']; ?></changefreq>
    </url>
<?php endforeach; ?>
<?php foreach ($articles as $a): ?>
    <url>
        <loc><?php echo SITE_URL . '/blog-post.php?slug=' . urlencode($a['slug']); ?></loc>
        <lastmod><?php echo date('c', strtotime($a['updated_at'] ?: $a['published_at'])); ?></lastmod>
        <priority>0.7</priority>
        <changefreq>monthly</changefreq>
    </url>
<?php endforeach; ?>
</urlset>
