<?php

declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../bootstrap.php';

use App\Core\{Database, Config};

$baseUrl = $_ENV['APP_URL'] ?? 'http://localhost/azraqtech';
$lastmod = date('Y-m-d');

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

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

$staticUrls = [
    ['url' => '/', 'changefreq' => 'weekly', 'priority' => '1.0'],
    ['url' => '/about', 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['url' => '/services', 'changefreq' => 'weekly', 'priority' => '0.9'],
    ['url' => '/industries', 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['url' => '/projects', 'changefreq' => 'monthly', 'priority' => '0.7'],
    ['url' => '/blog', 'changefreq' => 'weekly', 'priority' => '0.8'],
    ['url' => '/faq', 'changefreq' => 'monthly', 'priority' => '0.6'],
    ['url' => '/contact', 'changefreq' => 'monthly', 'priority' => '0.8'],
    ['url' => '/request-service', 'changefreq' => 'monthly', 'priority' => '0.7'],
    ['url' => '/request-quote', 'changefreq' => 'monthly', 'priority' => '0.7'],
    ['url' => '/equipment', 'changefreq' => 'monthly', 'priority' => '0.6'],
    ['url' => '/maintenance', 'changefreq' => 'monthly', 'priority' => '0.6'],
    ['url' => '/inspection-certification', 'changefreq' => 'monthly', 'priority' => '0.6'],
    ['url' => '/technical-consulting', 'changefreq' => 'monthly', 'priority' => '0.6'],
    ['url' => '/embedded-systems', 'changefreq' => 'monthly', 'priority' => '0.6'],
    ['url' => '/agricultural-machinery', 'changefreq' => 'monthly', 'priority' => '0.6'],
    ['url' => '/privacy-policy', 'changefreq' => 'yearly', 'priority' => '0.3'],
    ['url' => '/terms', 'changefreq' => 'yearly', 'priority' => '0.3'],
    ['url' => '/support', 'changefreq' => 'monthly', 'priority' => '0.5'],
];

foreach ($staticUrls as $page) {
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($baseUrl . $page['url']) . "</loc>\n";
    echo "    <lastmod>{$lastmod}</lastmod>\n";
    echo "    <changefreq>{$page['changefreq']}</changefreq>\n";
    echo "    <priority>{$page['priority']}</priority>\n";
    echo "  </url>\n";
}

$services = Database::fetchAll("SELECT slug, updated_at FROM services WHERE is_active = 1");
foreach ($services as $service) {
    $lastmod = date('Y-m-d', strtotime($service['updated_at']));
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($baseUrl . '/services/' . $service['slug']) . "</loc>\n";
    echo "    <lastmod>{$lastmod}</lastmod>\n";
    echo "    <changefreq>monthly</changefreq>\n";
    echo "    <priority>0.8</priority>\n";
    echo "  </url>\n";
}

$industries = Database::fetchAll("SELECT slug, updated_at FROM industries WHERE is_active = 1");
foreach ($industries as $industry) {
    $lastmod = date('Y-m-d', strtotime($industry['updated_at']));
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($baseUrl . '/industries/' . $industry['slug']) . "</loc>\n";
    echo "    <lastmod>{$lastmod}</lastmod>\n";
    echo "    <changefreq>monthly</changefreq>\n";
    echo "    <priority>0.7</priority>\n";
    echo "  </url>\n";
}

$projects = Database::fetchAll("SELECT slug, updated_at FROM projects WHERE status = 'completed'");
foreach ($projects as $project) {
    $lastmod = date('Y-m-d', strtotime($project['updated_at']));
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($baseUrl . '/projects/' . $project['slug']) . "</loc>\n";
    echo "    <lastmod>{$lastmod}</lastmod>\n";
    echo "    <changefreq>monthly</changefreq>\n";
    echo "    <priority>0.6</priority>\n";
    echo "  </url>\n";
}

$posts = Database::fetchAll("SELECT slug, published_at FROM blog_posts WHERE status = 'published'");
foreach ($posts as $post) {
    $lastmod = date('Y-m-d', strtotime($post['published_at']));
    echo "  <url>\n";
    echo "    <loc>" . htmlspecialchars($baseUrl . '/blog/' . $post['slug']) . "</loc>\n";
    echo "    <lastmod>{$lastmod}</lastmod>\n";
    echo "    <changefreq>monthly</changefreq>\n";
    echo "    <priority>0.7</priority>\n";
    echo "  </url>\n";
}

echo '</urlset>';