<?xml version="1.0" encoding="UTF-8"?>
<?php
/**
 * Dynamic Sitemap Generator for AhsapJaluzi.com
 * Google Search Console için SEO-uyumlu XML sitemap
 */

require_once 'core/db.php';
$db = getDB();

// Base URL
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$baseUrl = $protocol . '://' . ($_SERVER['HTTP_HOST'] ?? 'ahsapjaluzi.com');

// Current date for lastmod
$today = date('Y-m-d');
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    
    <!-- Homepage -->
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <!-- Main Product Page -->
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/index.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
    </url>
    
    <?php
    // Fetch all published products
    try {
        $stmt = $db->query("SELECT id, name, slug, image, updated_at FROM products WHERE status = 'active' ORDER BY order_num ASC, id ASC");
        $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($products as $product):
            $slug = $product['slug'] ?? 'urun-' . $product['id'];
            $lastmod = !empty($product['updated_at']) ? date('Y-m-d', strtotime($product['updated_at'])) : $today;
            $image = !empty($product['image']) ? htmlspecialchars($baseUrl . '/' . ltrim($product['image'], '/')) : '';
    ?>
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/?product=<?php echo htmlspecialchars($slug); ?></loc>
        <lastmod><?php echo $lastmod; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.9</priority>
        <?php if ($image): ?>
        <image:image>
            <image:loc><?php echo $image; ?></image:loc>
            <image:title><?php echo htmlspecialchars($product['name'] ?? 'Ürün'); ?></image:title>
        </image:image>
        <?php endif; ?>
    </url>
    <?php 
        endforeach;
    } catch (PDOException $e) {
        // Products table error - continue silently
    }
    ?>
    
    <?php
    // Fetch all categories
    try {
        $stmt = $db->query("SELECT id, name, slug FROM categories WHERE active = 1 ORDER BY id ASC");
        $categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($categories as $category):
            $slug = $category['slug'] ?? 'kategori-' . $category['id'];
    ?>
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/?cat=<?php echo htmlspecialchars($slug); ?></loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.8</priority>
    </url>
    <?php 
        endforeach;
    } catch (PDOException $e) {
        // Categories table error - continue silently
    }
    ?>
    
    <?php
    // Fetch all active blog posts
    try {
        $stmt = $db->query("SELECT id, title, slug, updated_at, created_at FROM blog_posts WHERE status = 'published' ORDER BY created_at DESC");
        $posts = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        foreach ($posts as $post):
            $slug = $post['slug'] ?? 'blog-' . $post['id'];
            $lastmod = !empty($post['updated_at']) ? date('Y-m-d', strtotime($post['updated_at'])) : 
                      (!empty($post['created_at']) ? date('Y-m-d', strtotime($post['created_at'])) : $today);
    ?>
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/blog-detail.php?slug=<?php echo htmlspecialchars($slug); ?></loc>
        <lastmod><?php echo $lastmod; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
    </url>
    <?php 
        endforeach;
    } catch (PDOException $e) {
        // Blog table error - continue silently
    }
    ?>
    
    <!-- Static Pages -->
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/faq.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/contact.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.6</priority>
    </url>
    
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/blog.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.6</priority>
    </url>
    
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/bank-accounts.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>
    
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/warranty.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.4</priority>
    </url>
    
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/shipping.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.4</priority>
    </url>
    
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/privacy.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.3</priority>
    </url>
    
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/career.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.4</priority>
    </url>
    
    <url>
        <loc><?php echo htmlspecialchars($baseUrl); ?>/order-tracking.php</loc>
        <lastmod><?php echo $today; ?></lastmod>
        <changefreq>yearly</changefreq>
        <priority>0.5</priority>
    </url>
    
</urlset>
