<?php
header('Content-Type: application/xml; charset=utf-8');

require_once __DIR__ . '/admin/includes/Database.php';
require_once __DIR__ . '/admin/includes/Setting.php';
require_once __DIR__ . '/admin/includes/News.php';
require_once __DIR__ . '/admin/includes/Product.php';
require_once __DIR__ . '/admin/includes/Project.php';
require_once __DIR__ . '/admin/includes/Service.php';
require_once __DIR__ . '/admin/includes/Staff.php';

try {
    $db = Database::getInstance();
    $setting = new Setting($db);
    $news = new News($db);
    $product = new Product();
    $project = new Project();
    $service = new Service($db);
    $staff = new Staff($db);
} catch (Exception $e) {
    error_log("Sitemap initialization error: " . $e->getMessage());
    header('HTTP/1.0 503 Service Unavailable');
    die('Service temporarily unavailable');
}

// Protocol ve domain (CLI'de test için varsayılan)
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https://' : 'http://';
$domain = $_SERVER['HTTP_HOST'] ?? 'localhost';
$base_url = $protocol . htmlspecialchars($domain);

// Static sayfaları tanımla
$static_pages = [
    '/' => 1.0,
    '/hizmetlerimiz' => 0.8,
    '/projelerimiz' => 0.8,
    '/personel' => 0.8,
    '/urunler' => 0.8,
    '/blog' => 0.8,
    '/iletisim' => 0.7,
    '/hakkimizda' => 0.7,
    '/duyurular' => 0.7,
];

// Blog yazılarını al
$blogs = [];
try {
    $blogs = $news->getAllNews(null, true) ?? [];
} catch (Exception $e) {
    error_log("Sitemap blogs error: " . $e->getMessage());
}

// Ürünleri al
$products = [];
try {
    $products = $product->getAllProducts(null, true) ?? [];
} catch (Exception $e) {
    error_log("Sitemap products error: " . $e->getMessage());
}

// Projeleri al
$projects = [];
try {
    // Project class'ının getAllProjects(int, bool) method imzasına uygun
    $projects = $project->getAllProjects(0, true) ?? [];
} catch (Exception $e) {
    error_log("Sitemap projects error: " . $e->getMessage());
}

// Hizmetleri al
$services = [];
try {
    $services = $service->getAllServices(null, true) ?? [];
} catch (Exception $e) {
    error_log("Sitemap services error: " . $e->getMessage());
}

// Personeli al
$staff_list = [];
try {
    $staff_list = $staff->getAllStaff(null, true) ?? [];
} catch (Exception $e) {
    error_log("Sitemap staff error: " . $e->getMessage());
}

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

// Static sayfalar
foreach ($static_pages as $page => $priority) {
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($base_url . $page) . "</loc>\n";
    echo "    <lastmod>" . date('Y-m-d') . "</lastmod>\n";
    echo "    <changefreq>weekly</changefreq>\n";
    echo "    <priority>" . $priority . "</priority>\n";
    echo "  </url>\n";
}

// Blog yazıları
foreach ($blogs as $blog) {
    if ($blog['is_active'] ?? false) {
        $blog_slug = $blog['slug'] ?? $blog['id'];
        echo "  <url>\n";
        echo "    <loc>" . htmlspecialchars($base_url . '/blog/' . $blog_slug) . "</loc>\n";
        echo "    <lastmod>" . htmlspecialchars($blog['updated_at'] ?? $blog['created_at'] ?? date('Y-m-d')) . "</lastmod>\n";
        echo "    <changefreq>monthly</changefreq>\n";
        echo "    <priority>0.7</priority>\n";
        echo "  </url>\n";
    }
}

// Ürünler
foreach ($products as $prod) {
    if ($prod['is_active'] ?? false) {
        $prod_slug = $prod['slug'] ?? $prod['id'];
        echo "  <url>\n";
        echo "    <loc>" . htmlspecialchars($base_url . '/urun/' . $prod_slug) . "</loc>\n";
        echo "    <lastmod>" . htmlspecialchars($prod['updated_at'] ?? $prod['created_at'] ?? date('Y-m-d')) . "</lastmod>\n";
        echo "    <changefreq>monthly</changefreq>\n";
        echo "    <priority>0.7</priority>\n";
        echo "  </url>\n";
    }
}

// Projeler
foreach ($projects as $proj) {
    if ($proj['is_active'] ?? false) {
        $proj_slug = $proj['slug'] ?? $proj['id'];
        echo "  <url>\n";
        echo "    <loc>" . htmlspecialchars($base_url . '/projelerimiz/' . $proj_slug) . "</loc>\n";
        echo "    <lastmod>" . htmlspecialchars($proj['updated_at'] ?? $proj['created_at'] ?? date('Y-m-d')) . "</lastmod>\n";
        echo "    <changefreq>monthly</changefreq>\n";
        echo "    <priority>0.7</priority>\n";
        echo "  </url>\n";
    }
}

// Hizmetler
foreach ($services as $svc) {
    if ($svc['is_active'] ?? false) {
        $svc_slug = $svc['slug'] ?? $svc['id'];
        echo "  <url>\n";
        echo "    <loc>" . htmlspecialchars($base_url . '/hizmetlerimiz/' . $svc_slug) . "</loc>\n";
        echo "    <lastmod>" . htmlspecialchars($svc['updated_at'] ?? $svc['created_at'] ?? date('Y-m-d')) . "</lastmod>\n";
        echo "    <changefreq>monthly</changefreq>\n";
        echo "    <priority>0.7</priority>\n";
        echo "  </url>\n";
    }
}

// Personel
foreach ($staff_list as $person) {
    if ($person['is_active'] ?? false) {
        $person_slug = $person['slug'] ?? $person['id'];
        echo "  <url>\n";
        echo "    <loc>" . htmlspecialchars($base_url . '/personel/' . $person_slug) . "</loc>\n";
        echo "    <lastmod>" . htmlspecialchars($person['updated_at'] ?? $person['created_at'] ?? date('Y-m-d')) . "</lastmod>\n";
        echo "    <changefreq>monthly</changefreq>\n";
        echo "    <priority>0.6</priority>\n";
        echo "  </url>\n";
    }
}

echo "</urlset>\n";
?>
