<?php
header('Content-Type: application/xml; charset=utf-8');


require_once 'includes/config.php';
require_once 'includes/functions.php';


$settings = getSiteSettings();
$domain = 'https://mvpvinc.com'; // Sabit domain kullan

echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
        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
        http://www.google.com/schemas/sitemap-image/1.1
        http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd">

    <!-- Homepage with images -->
    <url>
        <loc><?php echo $domain; ?>/</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
        <?php
        // Ana sayfa hero görselleri
        $hero_images = glob('uploads/pages/index.php_hero_*.{jpg,jpeg,png}', GLOB_BRACE);
        foreach (array_slice($hero_images, -5) as $image) { // Son 5 görsel
            echo "\n        <image:image>";
            echo "\n            <image:loc>" . $domain . "/" . $image . "</image:loc>";
            echo "\n            <image:caption>ANKARA SEPETLİ VİNÇ - Profesyonel Sepetli Vinç Kiralama Hizmetleri</image:caption>";
            echo "\n            <image:title>Ankara Sepetli Vinç Kiralama</image:title>";
            echo "\n        </image:image>";
        }
        ?>
    </url>

      <url>
        <loc><?php echo $domain; ?>/about.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
        <?php
        // Hakkımızda sayfası görselleri
        $about_images = glob('uploads/pages/about.php_about_*.{jpg,jpeg,png}', GLOB_BRACE);
        foreach (array_slice($about_images, -3) as $image) {
            echo "\n        <image:image>";
            echo "\n            <image:loc>" . $domain . "/" . $image . "</image:loc>";
            echo "\n            <image:caption>ANKARA SEPETLİ VİNÇ - Şirket Hakkında Bilgiler ve Deneyim</image:caption>";
            echo "\n            <image:title>Ankara Sepetli Vinç Hakkımızda</image:title>";
            echo "\n        </image:image>";
        }
        ?>
    </url>

     <url>
        <loc><?php echo $domain; ?>/services.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.8</priority>
        <?php
        
        $services_images = glob('uploads/pages/services.php_*.{jpg,jpeg,png}', GLOB_BRACE);
        foreach (array_slice($about_images, -3) as $image) {
            echo "\n        <image:image>";
            echo "\n            <image:loc>" . $domain . "/" . $image . "</image:loc>";
            echo "\n            <image:caption>ANKARA SEPETLİ VİNÇ - Şirketimiz ve Deneyimlerimiz</image:caption>";
            echo "\n            <image:title>Ankara Sepetli Vinç Hakkımızda</image:title>";
            echo "\n        </image:image>";
        }
        ?>    
    <url>
        <loc><?php echo $domain; ?>/gallery.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
        <?php
        // Galeri görselleri
        $gallery_images = glob('uploads/gallery/*.{jpg,jpeg,png}', GLOB_BRACE);
        foreach (array_slice($gallery_images, -10) as $image) { // Son 10 görsel
            echo "\n        <image:image>";
            echo "\n            <image:loc>" . $domain . "/" . $image . "</image:loc>";
            echo "\n            <image:caption>ANKARA SEPETLİ VİNÇ - Proje Fotoğrafları ve Çalışma Örnekleri</image:caption>";
            echo "\n            <image:title>Ankara Sepetli Vinç Proje Galerisi</image:title>";
            echo "\n        </image:image>";
        }
        ?>
    </url>

   
    <url>
        <loc><?php echo $domain; ?>/contact.php</loc>
        <lastmod><?php echo date('c'); ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
        <?php
        // İletişim sayfası görselleri
        $contact_images = glob('uploads/pages/contact.php_hero_*.{jpg,jpeg,png}', GLOB_BRACE);
        foreach (array_slice($contact_images, -2) as $image) {
            echo "\n        <image:image>";
            echo "\n            <image:loc>" . $domain . "/" . $image . "</image:loc>";
            echo "\n            <image:caption>ANKARA SEPETLİ VİNÇ - İletişim ve Konum Bilgileri</image:caption>";
            echo "\n            <image:title>Ankara Sepetli Vinç İletişim</image:title>";
            echo "\n        </image:image>";
        }
        ?>
    </url>

    <?php
   
    try {
        
        $stmt = $pdo->prepare("SELECT * FROM services WHERE status = 'active' ORDER BY id");
        $stmt->execute();
        $services = $stmt->fetchAll();
        
        foreach ($services as $service) {
            echo "    <url>\n";
            echo "        <loc>{$domain}/services.php#service-{$service['id']}</loc>\n";
            echo "        <lastmod>" . date('c', strtotime($service['updated_at'] ?? $service['created_at'])) . "</lastmod>\n";
            echo "        <changefreq>monthly</changefreq>\n";
            echo "        <priority>0.6</priority>\n";
            echo "    </url>\n";
        }
    } catch (Exception $e) {
        
    }

    try {
        
        $stmt = $pdo->prepare("SELECT * FROM gallery WHERE status = 'active' ORDER BY id");
        $stmt->execute();
        $gallery_items = $stmt->fetchAll();
        
        foreach ($gallery_items as $item) {
            echo "    <url>\n";
            echo "        <loc>{$domain}/gallery.php#item-{$item['id']}</loc>\n";
            echo "        <lastmod>" . date('c', strtotime($item['updated_at'] ?? $item['created_at'])) . "</lastmod>\n";
            echo "        <changefreq>monthly</changefreq>\n";
            echo "        <priority>0.5</priority>\n";
            echo "    </url>\n";
        }
    } catch (Exception $e) {
        
    }
    ?>

</urlset>
