const fs = require('fs');
const { SitemapStream, streamToPromise } = require('sitemap');
const path = require('path');


// ✅ Static pages
const staticLinks = [
    { url: '/', changefreq: 'daily', priority: 1.0 },
    { url: '/about', changefreq: 'daily', priority: 0.7 },
    { url: '/menu', changefreq: 'daily', priority: 0.7 },
    { url: '/careers', changefreq: 'weekly', priority: 0.7 },
    { url: '/blog', changefreq: 'weekly', priority: 0.7 },
    { url: '/contact', changefreq: 'monthly', priority: 0.5 }
];

// ✅ Dynamic blog posts (example)
const blogPosts = [
  { slug: 'the-flavors-of-north-india-more-than-just-curries' },
  { slug: 'the-secret-to-perfect-north-indian-curries' },
  { slug: 'the-rich-history-of-south-indian-cuisine' }
];

// Convert blog slugs to sitemap entries
const blogLinks = blogPosts.map(post => ({
  url: `/blog/${post.slug}`,
  changefreq: 'weekly',
  priority: 0.6
}));

const allLinks = [...staticLinks, ...blogLinks];

async function generateSitemap() {
    try {
        const sitemap = new SitemapStream({ hostname: 'https://shivasdosa.com/' });
        const writeStream = fs.createWriteStream(path.resolve(__dirname, '../public/sitemap.xml'));

        sitemap.pipe(writeStream);

        allLinks.forEach(link => sitemap.write(link));
        sitemap.end();

        await streamToPromise(sitemap);
        console.log('✅ sitemap.xml created with blog slugs!');
    } catch (error) {
        console.error('❌ Error creating sitemap.xml:', error);
    }
}

generateSitemap();
