<?php
// Save as: sitemap.php (generates XML)
require_once 'includes/config.php';
require_once 'includes/db.php';
require_once 'includes/functions.php';

header('Content-Type: application/xml; charset=utf-8');

$lastmod = date('Y-m-d');

// Static pages
$staticPages = [
    ['url' => '/',                        'priority' => '1.0',  'changefreq' => 'weekly'],
    ['url' => '/about.php',               'priority' => '0.8',  'changefreq' => 'monthly'],
    ['url' => '/contact.php',             'priority' => '0.9',  'changefreq' => 'monthly'],
    ['url' => '/team.php',                'priority' => '0.7',  'changefreq' => 'monthly'],
    ['url' => '/services/',               'priority' => '0.9',  'changefreq' => 'monthly'],
    ['url' => '/services/seo.php',        'priority' => '0.8',  'changefreq' => 'monthly'],
    ['url' => '/services/ppc-google-ads.php', 'priority' => '0.8', 'changefreq' => 'monthly'],
    ['url' => '/services/meta-ads.php',   'priority' => '0.8',  'changefreq' => 'monthly'],
    ['url' => '/services/social-media-marketing.php', 'priority' => '0.8', 'changefreq' => 'monthly'],
    ['url' => '/services/performance-marketing.php',  'priority' => '0.8', 'changefreq' => 'monthly'],
    ['url' => '/services/web-development.php',        'priority' => '0.7', 'changefreq' => 'monthly'],
    ['url' => '/services/local-seo.php',  'priority' => '0.8',  'changefreq' => 'monthly'],
    ['url' => '/services/content-marketing.php',      'priority' => '0.7', 'changefreq' => 'monthly'],
    ['url' => '/industries/',             'priority' => '0.8',  'changefreq' => 'monthly'],
    ['url' => '/industries/cafe-restaurant.php',      'priority' => '0.7', 'changefreq' => 'monthly'],
    ['url' => '/industries/salon-beauty.php',         'priority' => '0.7', 'changefreq' => 'monthly'],
    ['url' => '/industries/makeup-artist.php',        'priority' => '0.7', 'changefreq' => 'monthly'],
    ['url' => '/industries/healthcare.php',           'priority' => '0.7', 'changefreq' => 'monthly'],
    ['url' => '/industries/edtech.php',               'priority' => '0.7', 'changefreq' => 'monthly'],
    ['url' => '/industries/ecommerce.php',            'priority' => '0.7', 'changefreq' => 'monthly'],
    ['url' => '/offshore/',               'priority' => '0.8',  'changefreq' => 'monthly'],
    ['url' => '/internship/',             'priority' => '0.8',  'changefreq' => 'weekly'],
    ['url' => '/case-studies/',           'priority' => '0.8',  'changefreq' => 'weekly'],
    ['url' => '/blog/',                   'priority' => '0.8',  'changefreq' => 'daily'],
    ['url' => '/careers.php',             'priority' => '0.6',  'changefreq' => 'monthly'],
    ['url' => '/privacy-policy.php',      'priority' => '0.3',  'changefreq' => 'yearly'],
    ['url' => '/terms.php',               'priority' => '0.3',  'changefreq' => 'yearly'],
];

// Dynamic blog posts
$blogPosts = db()->fetchAll(
    "SELECT slug, updated_at FROM blog_posts WHERE status = 'published' ORDER BY published_at DESC"
);

// Dynamic case studies
$caseStudies = db()->fetchAll(
    "SELECT slug, created_at FROM case_studies WHERE status = 'published'"
);

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

    <?php foreach ($staticPages as $page): ?>
    <url>
        <loc><?= SITE_URL . htmlspecialchars($page['url']) ?></loc>
        <lastmod><?= $lastmod ?></lastmod>
        <changefreq><?= $page['changefreq'] ?></changefreq>
        <priority><?= $page['priority'] ?></priority>
    </url>
    <?php endforeach; ?>

    <?php foreach ($blogPosts as $post): ?>
    <url>
        <loc><?= SITE_URL ?>/blog/<?= htmlspecialchars($post['slug']) ?>.php</loc>
        <lastmod><?= date('Y-m-d', strtotime($post['updated_at'])) ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    <?php endforeach; ?>

    <?php foreach ($caseStudies as $cs): ?>
    <url>
        <loc><?= SITE_URL ?>/case-studies/<?= htmlspecialchars($cs['slug']) ?>.php</loc>
        <lastmod><?= date('Y-m-d', strtotime($cs['created_at'])) ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php endforeach; ?>

</urlset>