<?php
declare(strict_types=1);
/**
 * Sitemap Generator
 * 访问：/sitemap.xml（由 index.php / router.php / ThinkPHP 路由包含）
 */

use think\facade\Db;

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

try {
    $steuartAppReady = function_exists('app') && app()->initialized();
} catch (\Throwable $e) {
    $steuartAppReady = false;
}
if (!$steuartAppReady) {
    (new \think\App())->initialize();
}

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

$baseUrl = rtrim((string) env('APP_URL', 'https://www.steuart.top'), '/');

$lastmodDate = static function (?string $dt): string {
    if ($dt === null || $dt === '') {
        return gmdate('Y-m-d');
    }
    $ts = strtotime($dt);

    return $ts ? gmdate('Y-m-d', $ts) : gmdate('Y-m-d');
};

$indexMtime = @filemtime(__DIR__ . '/index.html') ?: time();
$staticDay  = gmdate('Y-m-d', $indexMtime);

// 静态页面
$staticPages = [
    ['loc' => '/', 'priority' => '1.0', 'changefreq' => 'daily'],
    ['loc' => '/products.html', 'priority' => '0.9', 'changefreq' => 'weekly'],
    ['loc' => '/news.html', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['loc' => '/cases.html', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['loc' => '/about.html', 'priority' => '0.6', 'changefreq' => 'monthly'],
    ['loc' => '/contact.html', 'priority' => '0.5', 'changefreq' => 'monthly'],
];

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

foreach ($staticPages as $page) {
    $xml .= '<url>';
    $xml .= '<loc>' . htmlspecialchars($baseUrl . $page['loc'], ENT_XML1 | ENT_QUOTES, 'UTF-8') . '</loc>';
    $xml .= '<lastmod>' . $staticDay . '</lastmod>';
    $xml .= '<changefreq>' . $page['changefreq'] . '</changefreq>';
    $xml .= '<priority>' . $page['priority'] . '</priority>';
    $xml .= '</url>';
}

try {
    $products = Db::name('products')->where('status', 1)->select()->toArray();
    foreach ($products as $p) {
        $lm = $lastmodDate($p['updated_at'] ?? $p['created_at'] ?? null);
        $xml .= '<url>';
        $xml .= '<loc>' . htmlspecialchars($baseUrl . '/product.html?id=' . (int) $p['id'], ENT_XML1 | ENT_QUOTES, 'UTF-8') . '</loc>';
        $xml .= '<lastmod>' . $lm . '</lastmod>';
        $xml .= '<changefreq>weekly</changefreq>';
        $xml .= '<priority>0.7</priority>';
        $xml .= '</url>';
    }

    $articles = Db::name('articles')->where('status', 1)->select()->toArray();
    foreach ($articles as $a) {
        $lm = $lastmodDate($a['updated_at'] ?? $a['created_at'] ?? null);
        $xml .= '<url>';
        $xml .= '<loc>' . htmlspecialchars($baseUrl . '/article.html?id=' . (int) $a['id'], ENT_XML1 | ENT_QUOTES, 'UTF-8') . '</loc>';
        $xml .= '<lastmod>' . $lm . '</lastmod>';
        $xml .= '<changefreq>weekly</changefreq>';
        $xml .= '<priority>0.7</priority>';
        $xml .= '</url>';
    }

    $cases = Db::name('cases')->where('status', 1)->select()->toArray();
    foreach ($cases as $c) {
        $lm = $lastmodDate($c['updated_at'] ?? $c['created_at'] ?? null);
        $xml .= '<url>';
        $xml .= '<loc>' . htmlspecialchars($baseUrl . '/case.html?id=' . (int) $c['id'], ENT_XML1 | ENT_QUOTES, 'UTF-8') . '</loc>';
        $xml .= '<lastmod>' . $lm . '</lastmod>';
        $xml .= '<changefreq>monthly</changefreq>';
        $xml .= '<priority>0.6</priority>';
        $xml .= '</url>';
    }
} catch (\Exception $e) {
    // 数据库未连接时只返回静态页面
}

$xml .= '</urlset>';

echo $xml;
