<?php
/**
 * ECWin工具站 Sitemap 动态生成
 * 自动从数据库获取所有工具和分类生成sitemap.xml
 */

require_once 'config.php';

// 设置响应头
header('Content-Type: application/xml; charset=UTF-8');
header('Cache-Control: public, max-age=3600');

// 获取数据库连接
$pdo = DatabaseConfig::getConnection();

// 获取所有活跃工具
$sql = "SELECT t.id, t.name, t.url, t.updated_at, t.created_at, c.slug as category_slug
        FROM tools t
        LEFT JOIN categories c ON t.category_id = c.id
        WHERE t.is_active = true
        ORDER BY t.created_at DESC";
$tools = DatabaseConfig::fetchAll($sql);

// 获取所有分类
$sql = "SELECT id, name, slug, updated_at FROM categories ORDER BY sort_order";
$categories = DatabaseConfig::fetchAll($sql);

// 网站基础URL
$baseUrl = 'https://www.ecwin.com';

// 子工具目录列表
$subTools = [
    'listdiff' => ['name' => '在线列表对比工具', 'priority' => '0.9'],
    'vlookup' => ['name' => '在线VLOOKUP数据查找', 'priority' => '0.9'],
    'Excel' => ['name' => 'Excel在线工具', 'priority' => '0.8'],
    'ymcx' => ['name' => '域名查询工具', 'priority' => '0.7'],
    'ymtq' => ['name' => '域名提取工具', 'priority' => '0.7'],
    'iptq' => ['name' => 'IP提取工具', 'priority' => '0.7'],
    'txt' => ['name' => '文本处理工具', 'priority' => '0.7'],
    'mac' => ['name' => 'MAC地址转换', 'priority' => '0.7'],
    'ziti' => ['name' => '字体工具', 'priority' => '0.6'],
    'koutu' => ['name' => '抠图工具', 'priority' => '0.6'],
    'fanqie' => ['name' => '番茄工具', 'priority' => '0.6'],
    'jmyljsq' => ['name' => '进制转换计算器', 'priority' => '0.7'],
    'baiban' => ['name' => '白板工具', 'priority' => '0.6']
];

// 输出XML
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
            xmlns:xhtml="http://www.w3.org/1999/xhtml"
            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">';

// 主页 - 最高优先级
echo '<url>';
echo '<loc>' . $baseUrl . '/</loc>';
echo '<lastmod>' . date('Y-m-d') . '</lastmod>';
echo '<changefreq>daily</changefreq>';
echo '<priority>1.0</priority>';
echo '</url>';

// 子工具页面 - 高优先级
foreach ($subTools as $slug => $info) {
    echo '<url>';
    echo '<loc>' . $baseUrl . '/' . $slug . '/</loc>';
    echo '<lastmod>' . date('Y-m-d') . '</lastmod>';
    echo '<changefreq>weekly</changefreq>';
    echo '<priority>' . $info['priority'] . '</priority>';
    echo '</url>';
}

// 分类页面
foreach ($categories as $category) {
    if (in_array($category['slug'], ['new', 'hot', 'recent'])) continue;

    echo '<url>';
    echo '<loc>' . $baseUrl . '/#category-' . htmlspecialchars($category['slug']) . '</loc>';
    echo '<lastmod>' . ($category['updated_at'] ?? date('Y-m-d')) . '</lastmod>';
    echo '<changefreq>weekly</changefreq>';
    echo '<priority>0.6</priority>';
    echo '</url>';
}

// 管理后台工具详情（如果有外部链接的工具）
foreach ($tools as $tool) {
    // 只输出内部工具的URL
    if (!$tool['is_external']) {
        $toolUrl = $tool['url'];
        if (!empty($toolUrl) && !preg_match('/^http/', $toolUrl)) {
            $fullUrl = $baseUrl . $toolUrl;
            echo '<url>';
            echo '<loc>' . htmlspecialchars($fullUrl) . '</loc>';
            echo '<lastmod>' . ($tool['updated_at'] ?? $tool['created_at'] ?? date('Y-m-d')) . '</lastmod>';

            // /t/ 目录下的工具页面优先级更高（独立SEO页面）
            if (strpos($toolUrl, '/t/') === 0) {
                echo '<changefreq>weekly</changefreq>';
                echo '<priority>0.8</priority>';
                // hreflang 中英双语支持
                echo '<xhtml:link rel="alternate" hreflang="zh-CN" href="' . htmlspecialchars($fullUrl) . '"/>';
                echo '<xhtml:link rel="alternate" hreflang="en" href="' . htmlspecialchars($fullUrl) . '?lang=en"/>';
            } else {
                echo '<changefreq>monthly</changefreq>';
                echo '<priority>0.5</priority>';
            }

            echo '</url>';
        }
    }
}

echo '</urlset>';
?>