<?php
// ══════════════════════════════════════════════════════════════════════════
// SITEMAP — BLACK LIVES MATTER (blacklivesmatter.support)
// File: sitemap.php
// Static pages + one entry for every published missing-child case.
// Google reads this and discovers new cases without anyone typing a thing.
// ══════════════════════════════════════════════════════════════════════════

// ── CREDENTIALS (same database as missing.php) ──
$DB_HOST = 'localhost';
$DB_NAME = 'blmsupport_news';
$DB_USER = 'blmsupport_news-wizard';
$DB_PASS = '$Yllogism3825968';

$SITE = 'https://blacklivesmatter.support';

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

function xe($s){ return htmlspecialchars($s ?? '', ENT_QUOTES | ENT_XML1, 'UTF-8'); }

function slugify($s) {
    $s = strtolower(trim($s ?? ''));
    $s = preg_replace('/[^a-z0-9]+/', '-', $s);
    return trim($s, '-');
}

// ══════════════════════════════════════════════════════════════════════════
// 1. STATIC PAGES
// ══════════════════════════════════════════════════════════════════════════

$today = date('Y-m-d');

$static = [
    ['/index.php',                        $today, 'daily',   '1.0'],
    ['/missing.php',                      $today, 'daily',   '0.9'],
    ['/search.php',                       $today, 'daily',   '0.9'],
    ['/blog/',                            $today, 'daily',   '0.9'],
    ['/topics/child-trafficking.php',     $today, 'monthly', '0.8'],
    ['/topics/online-exploitation.php',   $today, 'monthly', '0.8'],
    ['/topics/child-abduction.php',       $today, 'monthly', '0.8'],
    ['/topics/grooming.php',              $today, 'monthly', '0.8'],
    ['/topics/csam.php',                  $today, 'monthly', '0.8'],
    ['/topics/child-labor.php',           $today, 'monthly', '0.8'],
    ['/legal/privacy.php',                $today, 'yearly',  '0.3'],
    ['/legal/terms.php',                  $today, 'yearly',  '0.3'],
];

// ══════════════════════════════════════════════════════════════════════════
// 2. CASE PAGES
// ══════════════════════════════════════════════════════════════════════════

$cases = [];
try {
    $pdo = new PDO(
        "mysql:host=$DB_HOST;dbname=$DB_NAME;charset=utf8mb4",
        $DB_USER, $DB_PASS,
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );
    $stmt = $pdo->query(
        "SELECT full_name, case_number, missing_date, created_at
           FROM missing_persons
          WHERE is_published = 1
            AND case_number IS NOT NULL
            AND case_number <> ''
          ORDER BY missing_date DESC
          LIMIT 45000"
    );
    $cases = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    // A database hiccup must not produce a broken sitemap.
    // Static pages still ship; cases return on the next crawl.
    error_log('sitemap.php DB error: ' . $e->getMessage());
}

// ══════════════════════════════════════════════════════════════════════════
// 3. OUTPUT
// ══════════════════════════════════════════════════════════════════════════

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

foreach ($static as $row) {
    list($path, $mod, $freq, $pri) = $row;
    echo "  <url>\n";
    echo "    <loc>" . xe($SITE . $path) . "</loc>\n";
    echo "    <lastmod>" . xe($mod) . "</lastmod>\n";
    echo "    <changefreq>" . xe($freq) . "</changefreq>\n";
    echo "    <priority>" . xe($pri) . "</priority>\n";
    echo "  </url>\n";
}

foreach ($cases as $c) {
    $slug = slugify($c['full_name'] ?? '');
    if ($slug === '') $slug = 'missing-child';

    $loc = $SITE . '/cases/' . $slug . '--' . rawurlencode($c['case_number']);

    $stamp = $c['created_at'] ?? $c['missing_date'] ?? null;
    $mod   = $stamp ? date('Y-m-d', strtotime($stamp)) : $today;

    echo "  <url>\n";
    echo "    <loc>" . xe($loc) . "</loc>\n";
    echo "    <lastmod>" . xe($mod) . "</lastmod>\n";
    echo "    <changefreq>weekly</changefreq>\n";
    echo "    <priority>0.7</priority>\n";
    echo "  </url>\n";
}

echo '</urlset>' . "\n";