<?php
// sitemap.xml - Dynamic Sitemap Generator for AvoPrime
header('Content-Type: application/xml; charset=utf-8');

$baseUrl = 'https://avoprimeltd.com';

// Get blog post dates for lastmod
$db = new SQLite3('avoprime.db');
$blogDates = [];
$blogs = $db->query("SELECT created_at FROM blog_posts WHERE is_published=1 ORDER BY created_at DESC LIMIT 1");
if ($blog = $blogs->fetchArray(SQLITE3_ASSOC)) {
    $blogLastMod = date('Y-m-d', strtotime($blog['created_at']));
} else {
    $blogLastMod = date('Y-m-d');
}

$today = date('Y-m-d');

// Define all pages with priority and change frequency
$pages = [
    // Primary pages
    ['url' => '/', 'priority' => '1.00', 'changefreq' => 'weekly', 'lastmod' => $today],
    ['url' => '/about-us', 'priority' => '0.90', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/products', 'priority' => '0.90', 'changefreq' => 'weekly', 'lastmod' => $today],
    ['url' => '/services', 'priority' => '0.90', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/gallery', 'priority' => '0.80', 'changefreq' => 'weekly', 'lastmod' => $today],
    ['url' => '/contact', 'priority' => '0.80', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/blogs', 'priority' => '0.80', 'changefreq' => 'weekly', 'lastmod' => $blogLastMod],
    ['url' => '/documents', 'priority' => '0.70', 'changefreq' => 'monthly', 'lastmod' => $today],
    
    // Secondary/anchor pages
    ['url' => '/about-us#who-we-are', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/about-us#full-vision', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/about-us#full-mission', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/about-us#core-values', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/about-us#leadership', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/about-us#timeline', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/products#product-categories', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/products#specifications', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/products#byproducts', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/products#applications', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/products#packaging', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/products#certifications', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/services#core-services', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/services#processing', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/services#export-services', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/services#private-label', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/services#pricing', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/gallery#video-gallery', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/gallery#gallery-stats', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/contact#contact-info', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/contact#inquiry-form', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/contact#office-locations', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/contact#business-hours', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/contact#map-section', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/blogs#blog-categories', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $blogLastMod],
    ['url' => '/documents#doc-categories', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/documents#documents-table', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/documents#cert-showcase', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/documents#policies', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
    ['url' => '/documents#reports', 'priority' => '0.60', 'changefreq' => 'monthly', 'lastmod' => $today],
];

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"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

<?php foreach ($pages as $page): ?>
  <url>
    <loc><?php echo $baseUrl . $page['url']; ?></loc>
    <lastmod><?php echo $page['lastmod']; ?></lastmod>
    <changefreq><?php echo $page['changefreq']; ?></changefreq>
    <priority><?php echo $page['priority']; ?></priority>
  </url>
<?php endforeach; ?>

</urlset>