<!DOCTYPE html>
<html>
<head>
    <title>Sitemap Generator - Stoke Speaks</title>
</head>
<body>
    <h1>Stoke Speaks - Sitemap Generator</h1>
    <p>This page generates a dynamic sitemap from your API.</p>
    <pre id="sitemap-output">Generating sitemap...</pre>

    <script>
        const API_URL = 'https://stoke-backend.mazbutt.workers.dev';
        const SITE_URL = 'https://stokespeaks.justlocal.uk';

        async function generateSitemap() {
            const output = document.getElementById('sitemap-output');
            
            try {
                // Fetch all posts from API
                const response = await fetch(`${API_URL}/api/posts`);
                const posts = await response.json();
                
                let sitemap = `<?xml version="1.0" encoding="UTF-8"?>\n`;
                sitemap += `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n`;
                
                // Add homepage
                sitemap += `    <url>\n`;
                sitemap += `        <loc>${SITE_URL}/</loc>\n`;
                sitemap += `        <lastmod>${new Date().toISOString().split('T')[0]}</lastmod>\n`;
                sitemap += `        <changefreq>daily</changefreq>\n`;
                sitemap += `        <priority>1.0</priority>\n`;
                sitemap += `    </url>\n`;
                
                // Add each post
                posts.forEach(post => {
                    sitemap += `    <url>\n`;
                    sitemap += `        <loc>${SITE_URL}/post/${post.id}</loc>\n`;
                    const postDate = post.created_at ? new Date(post.created_at).toISOString().split('T')[0] : new Date().toISOString().split('T')[0];
                    sitemap += `        <lastmod>${postDate}</lastmod>\n`;
                    sitemap += `        <changefreq>weekly</changefreq>\n`;
                    sitemap += `        <priority>0.7</priority>\n`;
                    sitemap += `    </url>\n`;
                });
                
                // Add councillor pages
                const councillors = [
                    "Abi-Brown", "Adrian-Knapper", "Alastair-Watson", "Amjid-Wazir", 
                    "Andy-Platt", "Carl-Edwards", "Chandra-Kanneganti", "Chris-Robinson",
                    "Craig-Beardmore", "Daniela-Santoro", "Daniel-Jellyman", "Dave-Evans",
                    "David-Mountford", "David-Williams", "Diane-Williams", "Duncan-Walker",
                    "Faisal-Hussain", "Finlay-Gordon-McCusker", "Glen-Watson", "Gurmeet-Singh-Kallar",
                    "Heather-Blurton", "Jane-Ashworth", "Javid-Najmi", "Joan-Bell",
                    "Laura-Carter", "Lauren-Davison", "Lilian-Dodd", "Lorraine-Beardmore",
                    "Lyn-Sharpe", "Lynn-Watkins", "Majid-Khan", "Maxine-Clark",
                    "Mubsira-Aumir", "Rachel-Kelsall", "Ross-Irving", "Sadaqat-Maqsoom",
                    "Sarah-Hill", "Sarah-Jane-Colclough", "Shaun-Pender", "Steve-Watkins",
                    "Sue-Akkurt", "Tabrase-Din", "Waseem-Akbar"
                ];
                
                councillors.forEach(councillor => {
                    sitemap += `    <url>\n`;
                    sitemap += `        <loc>${SITE_URL}/#councillor/${councillor.toLowerCase()}</loc>\n`;
                    sitemap += `        <lastmod>${new Date().toISOString().split('T')[0]}</lastmod>\n`;
                    sitemap += `        <changefreq>monthly</changefreq>\n`;
                    sitemap += `        <priority>0.5</priority>\n`;
                    sitemap += `    </url>\n`;
                });
                
                // Add six towns
                const towns = ['hanley', 'tunstall', 'burslem', 'fenton', 'longton', 'stoke'];
                towns.forEach(town => {
                    sitemap += `    <url>\n`;
                    sitemap += `        <loc>${SITE_URL}/?area=${town}</loc>\n`;
                    sitemap += `        <lastmod>${new Date().toISOString().split('T')[0]}</lastmod>\n`;
                    sitemap += `        <changefreq>weekly</changefreq>\n`;
                    sitemap += `        <priority>0.6</priority>\n`;
                    sitemap += `    </url>\n`;
                });
                
                sitemap += `</urlset>`;
                
                output.textContent = sitemap;
                
                // Create download link
                const downloadLink = document.createElement('a');
                downloadLink.href = 'data:text/xml;charset=utf-8,' + encodeURIComponent(sitemap);
                downloadLink.download = 'sitemap.xml';
                downloadLink.textContent = '📥 Download sitemap.xml';
                downloadLink.style.display = 'block';
                downloadLink.style.marginTop = '20px';
                downloadLink.style.padding = '10px 20px';
                downloadLink.style.background = '#C9A84C';
                downloadLink.style.color = '#0F1A2E';
                downloadLink.style.borderRadius = '8px';
                downloadLink.style.textDecoration = 'none';
                downloadLink.style.fontWeight = 'bold';
                downloadLink.style.textAlign = 'center';
                output.parentNode.appendChild(downloadLink);
                
            } catch (error) {
                output.textContent = 'Error generating sitemap: ' + error.message;
                console.error(error);
            }
        }
        
        generateSitemap();
    </script>
</body>
</html>