<?php
/**
 * Funky Developers - HTML Sitemap
 */

define('SITE_ROOT', __DIR__);
require_once __DIR__ . '/includes/config.php';

$pageTitle = 'Sitemap | Funky Developers';
$pageDescription = 'Site map of all pages on Funky Developers website.';
$canonicalUrl = SITE_URL . '/sitemap';

// Get all published pages (exclude real estate city pages - they have their own section)
$pages = dbFetchAll("SELECT slug, title FROM pages WHERE status = 'published' AND slug NOT LIKE 'real-estate-lead-generation-%' ORDER BY title ASC");

include __DIR__ . '/includes/header.php';
?>

<section class="page-header">
    <div class="container">
        <h1>Sitemap</h1>
        <p>All pages on our website</p>
    </div>
</section>

<section class="page-content">
    <div class="container">
        <div class="content-wrapper">
            <h2>XML Sitemaps (for Search Engines)</h2>
            <ul style="list-style: none; padding: 0;">
                <li style="margin: 0.5rem 0;"><a href="<?php echo SITE_URL; ?>/sitemap-index.xml">Sitemap Index</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/sitemap-pages.xml">Pages</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/sitemap-blog.xml">Blog</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/sitemap-categories.xml">Categories</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/sitemap-tags.xml">Tags</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/sitemap-case-studies.xml">Case Studies</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/sitemap-media.xml">Media (Images)</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/sitemap-video.xml">Video</a></li>
            </ul>
            
            <h2 style="margin-top: 2rem;">Main Pages</h2>
            <ul style="list-style: none; padding: 0;">
                <li style="margin: 0.5rem 0;"><a href="<?php echo SITE_URL; ?>">Home</a></li>
                <li style="margin: 0.5rem 0;"><a href="<?php echo SITE_URL; ?>/about-us">About Us</a></li>
                <li style="margin: 0.5rem 0;"><a href="<?php echo SITE_URL; ?>/our-founder">Our Founder</a></li>
                <li style="margin: 0.5rem 0;"><a href="<?php echo SITE_URL; ?>/clients">Clients</a></li>
                <li style="margin: 0.5rem 0;"><a href="<?php echo SITE_URL; ?>/career">Career</a></li>
                <li style="margin: 0.5rem 0;"><a href="<?php echo SITE_URL; ?>/contact-us">Contact Us</a></li>
            </ul>
            
            <h2 style="margin-top: 2rem;">Service Pages</h2>
            <ul style="list-style: none; padding: 0;">
                <li style="margin: 0.5rem 0;"><strong>Marketing Services:</strong></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/performance-marketing">Performance Marketing</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/digital-marketing">Digital Marketing Agency</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/seo">Search Engine Optimization (SEO)</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/lead-generation">Lead Generation Services</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/advertisement">Advertisement</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/outdoor-media">Outdoor Media</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/ambient-media">Ambient Media</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/public-relation">Public Relation</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/social-media-awareness">Social Media Awareness</a></li>
                
                <li style="margin: 1rem 0;"><strong>Design Services:</strong></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/video-animation">Video Animation</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/ugc-videos">UGC Videos</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/graphic-designing">Graphic Designing</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/logo-designing">Logo Designing</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/company-profile-design">Company Profile Design</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/voice-over-artist">Voice Over Artist</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/packaging-design">Packaging & Design</a></li>
                
                <li style="margin: 1rem 0;"><strong>Technology Services:</strong></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/website-development">Website Development</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/mobile-app-development">Mobile App Development</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/software-consultation">Software Consultation</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/software-development">Software Development</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/crm-erp-software">CRM/ERP Software</a></li>
                <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/hire-developers">Hire Developers</a></li>
            </ul>
            
            <h2 style="margin-top: 2rem;">Blog</h2>
            <ul style="list-style: none; padding: 0;">
                <li style="margin: 0.5rem 0;"><a href="<?php echo SITE_URL; ?>/blog">Blog</a></li>
                <?php
                $blogCategories = [];
                try {
                    $blogCategories = dbFetchAll("SELECT slug, name FROM blog_categories WHERE status = 1 ORDER BY name ASC");
                } catch (Exception $e) {
                    $blogCategories = [];
                }
                foreach ($blogCategories as $category): ?>
                    <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/blog?category=<?php echo escapeOutput($category['slug']); ?>"><?php echo escapeOutput($category['name'] ?? $category['slug']); ?></a></li>
                <?php endforeach; ?>
                <?php
                $blogTags = [];
                try {
                    $postsWithTags = dbFetchAll("SELECT tags FROM blog_posts WHERE status = 'published' AND tags IS NOT NULL AND tags != '' AND tags != '[]'");
                    foreach ($postsWithTags as $row) {
                        $tags = json_decode($row['tags'], true);
                        if (is_array($tags)) {
                            foreach ($tags as $tag) {
                                $tag = trim($tag);
                                if ($tag !== '') $blogTags[$tag] = true;
                            }
                        }
                    }
                    ksort($blogTags);
                } catch (Exception $e) {
                    $blogTags = [];
                }
                foreach (array_keys($blogTags) as $tag): ?>
                    <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/blog?tag=<?php echo htmlspecialchars(urlencode($tag), ENT_QUOTES, 'UTF-8'); ?>"><?php echo escapeOutput($tag); ?></a></li>
                <?php endforeach; ?>
                <?php
                $authors = [];
                try {
                    $authors = dbFetchAll("SELECT slug, name FROM blog_authors WHERE status = 1 ORDER BY name ASC");
                } catch (Exception $e) {
                    $authors = [];
                }
                foreach ($authors as $author): ?>
                    <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/author/<?php echo escapeOutput($author['slug']); ?>"><?php echo escapeOutput($author['name'] ?? $author['slug']); ?></a></li>
                <?php endforeach; ?>
                <?php
                $blogPosts = [];
                try {
                    $blogPosts = dbFetchAll("SELECT slug, title FROM blog_posts WHERE status = 'published' ORDER BY published_at DESC LIMIT 20");
                } catch (Exception $e) {
                    $blogPosts = [];
                }
                foreach ($blogPosts as $post): ?>
                    <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/blog/<?php echo escapeOutput($post['slug']); ?>"><?php echo escapeOutput($post['title'] ?? $post['slug']); ?></a></li>
                <?php endforeach; ?>
            </ul>
            
            <h2 style="margin-top: 2rem;">Case Studies</h2>
            <ul style="list-style: none; padding: 0;">
                <li style="margin: 0.5rem 0;"><a href="<?php echo SITE_URL; ?>/case-studies">Case Studies</a></li>
                <?php
                $caseStudyTags = [];
                try {
                    $studiesWithTags = dbFetchAll("SELECT tags FROM case_studies WHERE status = 'published' AND tags IS NOT NULL AND tags != '' AND tags != '[]'");
                    foreach ($studiesWithTags as $row) {
                        $tags = json_decode($row['tags'], true);
                        if (is_array($tags)) {
                            foreach ($tags as $tag) {
                                $tag = trim($tag);
                                if ($tag !== '') $caseStudyTags[$tag] = true;
                            }
                        }
                    }
                    ksort($caseStudyTags);
                } catch (Exception $e) {
                    $caseStudyTags = [];
                }
                foreach (array_keys($caseStudyTags) as $tag): ?>
                    <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/case-studies?tag=<?php echo htmlspecialchars(urlencode($tag), ENT_QUOTES, 'UTF-8'); ?>"><?php echo escapeOutput($tag); ?></a></li>
                <?php endforeach; ?>
                <?php
                $caseStudies = [];
                try {
                    $caseStudies = dbFetchAll("SELECT id, slug, title FROM case_studies WHERE status = 'published' ORDER BY date DESC");
                } catch (Exception $e) {
                    $caseStudies = [];
                }
                foreach ($caseStudies as $caseStudy):
                    $slug = !empty($caseStudy['slug']) ? $caseStudy['slug'] : 'case-study-' . ($caseStudy['id'] ?? '');
                    $title = $caseStudy['title'] ?? $slug;
                ?>
                    <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/case-study/<?php echo escapeOutput($slug); ?>"><?php echo escapeOutput($title); ?></a></li>
                <?php endforeach; ?>
            </ul>
            
            <h2 style="margin-top: 2rem;">Real Estate Lead Generation</h2>
            <ul style="list-style: none; padding: 0;">
                <li style="margin: 0.5rem 0;"><a href="<?php echo SITE_URL; ?>/real-estate-lead-generation">Real Estate Lead Generation</a></li>
                <?php
                $addedCitySlugs = [];
                $realEstateCities = [];
                try {
                    $conn = getDB();
                    $tableCheck = $conn->query("SHOW TABLES LIKE 'city_real_estate_data'");
                    if ($tableCheck && $tableCheck->num_rows > 0) {
                        $cityData = dbFetchAll("SELECT city_slug, city_name FROM city_real_estate_data WHERE status = 'active' ORDER BY city_name ASC");
                        foreach ($cityData as $city) {
                            $realEstateCities[] = ['slug' => $city['city_slug'], 'title' => $city['city_name'] ?? $city['city_slug']];
                            $addedCitySlugs[$city['city_slug']] = true;
                        }
                    }
                } catch (Exception $e) {
                    // Table doesn't exist - continue
                }
                try {
                    $pagesFromPages = dbFetchAll("SELECT slug, title FROM pages WHERE status = 'published' AND slug LIKE 'real-estate-lead-generation-%' ORDER BY slug ASC");
                    foreach ($pagesFromPages as $cityPage) {
                        $citySlug = str_replace('real-estate-lead-generation-', '', $cityPage['slug']);
                        if (isset($addedCitySlugs[$citySlug])) continue;
                        $realEstateCities[] = ['slug' => $cityPage['slug'], 'title' => $cityPage['title'] ?? $citySlug];
                    }
                } catch (Exception $e) {
                    // Query failed - continue
                }
                foreach ($realEstateCities as $city): ?>
                    <li style="margin: 0.5rem 0; padding-left: 1rem;"><a href="<?php echo SITE_URL; ?>/<?php echo escapeOutput(strpos($city['slug'], 'real-estate') === 0 ? $city['slug'] : 'real-estate-lead-generation-' . $city['slug']); ?>"><?php echo escapeOutput($city['title']); ?></a></li>
                <?php endforeach; ?>
            </ul>
            
            <?php
            // Get policy pages
            $policies = [];
            try {
                $policies = dbFetchAll("SELECT slug, title FROM policies");
            } catch (Exception $e) {
                $policies = [];
            }
            ?>
            
            <?php if (!empty($policies)): ?>
                <h2 style="margin-top: 2rem;">Policy Pages</h2>
                <ul style="list-style: none; padding: 0;">
                    <?php foreach ($policies as $policy): ?>
                        <li style="margin: 0.5rem 0;">
                            <a href="<?php echo SITE_URL . '/' . escapeOutput($policy['slug']); ?>">
                                <?php echo escapeOutput($policy['title']); ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
            
            <?php if (!empty($pages)): ?>
                <h2 style="margin-top: 2rem;">Other Pages</h2>
                <ul style="list-style: none; padding: 0;">
                    <?php foreach ($pages as $page): ?>
                        <li style="margin: 0.5rem 0;">
                            <a href="<?php echo SITE_URL . '/' . escapeOutput($page['slug']); ?>">
                                <?php echo escapeOutput($page['title']); ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </div>
    </div>
</section>

<?php include __DIR__ . '/includes/footer.php'; ?>

