<?php
/**
 * TG4G English sitemap (/en/sitemap.xml)
 *
 * - /en/sitemap.xml          -> index (cats + N shards)
 * - /en/sitemap.xml?p=cats   -> 17 EN category pages + EN homepage
 * - /en/sitemap.xml?p=N      -> Nth shard of EN listing detail URLs (50,000 / shard)
 *
 * Mirror of /var/www/tg4g/public/sitemap.php, points to /en/ URLs only.
 *
 * Deploy to: /var/www/tg4g/public/en/sitemap.php
 *
 * REMEMBER: also reference this from the root sitemap index so search engines
 * pick it up. See nginx_en.txt / DEPLOY_en.txt for the patch.
 */
declare(strict_types=1);

$cfg = require '/var/www/tg4g/config.php';
$db = $cfg['database'];
$dsn = 'mysql:host=' . $db['host'] . ';port=' . ($db['port'] ?? 3306) . ';dbname=' . $db['database'] . ';charset=utf8mb4';
$pdo = new PDO($dsn, $db['username'], $db['password'], [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);

const SITE = 'https://tg4g.com';
const PER  = 50000;
const CATS = ['vps','dedicated','cdn','payment','proxy','domain','saas','ai','edu','dev','crypto','marketing','design','security','biz','comm','ec'];

header('Content-Type: application/xml; charset=utf-8');
header('Cache-Control: public, max-age=3600');

$part = $_GET['p'] ?? '';
function xe($s) { return htmlspecialchars((string)$s, ENT_QUOTES|ENT_XML1, 'UTF-8'); }

// hreflang block builder (always en + zh-CN pair)
function alt_block(string $zh_url, string $en_url): string {
    return '<xhtml:link rel="alternate" hreflang="en"    href="' . xe($en_url) . '"/>'
         . '<xhtml:link rel="alternate" hreflang="zh-CN" href="' . xe($zh_url) . '"/>'
         . '<xhtml:link rel="alternate" hreflang="x-default" href="' . xe($en_url) . '"/>';
}

// ============ 1. Index ============
if ($part === '') {
    $total = (int)$pdo->query("SELECT COUNT(*) FROM listings WHERE is_active=1")->fetchColumn();
    $shards = max(1, (int)ceil($total / PER));
    $latest = $pdo->query("SELECT MAX(updated_at) FROM listings WHERE is_active=1")->fetchColumn() ?: date('c');
    $lastmod = date('Y-m-d', strtotime($latest));

    echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
    echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
    echo "  <sitemap><loc>" . SITE . "/en/sitemap.xml?p=cats</loc><lastmod>$lastmod</lastmod></sitemap>\n";
    for ($i = 1; $i <= $shards; $i++) {
        echo "  <sitemap><loc>" . SITE . "/en/sitemap.xml?p=$i</loc><lastmod>$lastmod</lastmod></sitemap>\n";
    }
    echo '</sitemapindex>';
    exit;
}

// ============ 2. Cats + EN homepage ============
if ($part === 'cats') {
    echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
    echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">' . "\n";
    $today = date('Y-m-d');

    // Homepage
    echo "  <url><loc>" . SITE . "/en/</loc>"
       . alt_block(SITE . '/', SITE . '/en/')
       . "<lastmod>$today</lastmod><changefreq>daily</changefreq><priority>1.0</priority></url>\n";

    // Directory landing
    echo "  <url><loc>" . SITE . "/en/listings</loc>"
       . alt_block(SITE . '/listings', SITE . '/en/listings')
       . "<lastmod>$today</lastmod><changefreq>daily</changefreq><priority>0.9</priority></url>\n";

    // 17 categories
    foreach (CATS as $c) {
        $en = SITE . "/en/listings?cat=$c";
        $zh = SITE . "/listings?cat=$c";
        echo "  <url><loc>" . xe($en) . "</loc>" . alt_block($zh, $en) . "<lastmod>$today</lastmod><changefreq>daily</changefreq><priority>0.8</priority></url>\n";
    }

    // Hot sub_category pages (>= 5)
    $sub_st = $pdo->query("SELECT category, sub_category, COUNT(*) AS n FROM listings WHERE is_active=1 AND sub_category IS NOT NULL AND sub_category<>'' GROUP BY category, sub_category HAVING n >= 5 ORDER BY category, n DESC");
    foreach ($sub_st->fetchAll() as $row) {
        $qs = http_build_query(['cat' => $row['category'], 'sub' => $row['sub_category']]);
        $en = SITE . '/en/listings?' . $qs;
        $zh = SITE . '/listings?' . $qs;
        echo "  <url><loc>" . xe($en) . "</loc>" . alt_block($zh, $en) . "<lastmod>$today</lastmod><changefreq>weekly</changefreq><priority>0.6</priority></url>\n";
    }
    echo '</urlset>';
    exit;
}

// ============ 3. Detail shards ============
$shard = max(1, (int)$part);
$offset = ($shard - 1) * PER;

$st = $pdo->prepare("SELECT id, updated_at FROM listings WHERE is_active=1 ORDER BY id ASC LIMIT " . PER . " OFFSET $offset");
$st->execute();
$rows = $st->fetchAll();

if (!$rows) {
    http_response_code(404);
    echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"/>";
    exit;
}

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">' . "\n";
foreach ($rows as $r) {
    $lastmod = $r['updated_at'] ? date('Y-m-d', strtotime($r['updated_at'])) : date('Y-m-d');
    $id = (int)$r['id'];
    $en = SITE . "/en/listing/$id";
    $zh = SITE . "/listing/$id";
    echo "  <url><loc>" . $en . "</loc>"
       . alt_block($zh, $en)
       . "<lastmod>$lastmod</lastmod><changefreq>weekly</changefreq><priority>0.5</priority></url>\n";
}
echo '</urlset>';
