<?php
ini_set('display_errors', '0');
ob_start();
require_once __DIR__ . '/db.php';

$base = 'https://hentaiworld.site';

function x($s) {
    $s = preg_replace('/[^\x{9}\x{A}\x{D}\x{20}-\x{D7FF}\x{E000}-\x{FFFD}]/u', '', (string)$s);
    return htmlspecialchars((string)$s, ENT_XML1 | ENT_QUOTES, 'UTF-8');
}
function cut($s, $n) { return function_exists('mb_substr') ? mb_substr($s, 0, $n) : substr($s, 0, $n); }
function slugify($text, $max = 75) {
    $s = strtolower(trim($text));
    $s = preg_replace('/[^a-z0-9]+/i', '-', $s);
    $s = preg_replace('/-+/', '-', $s);
    $s = trim($s, '-');
    if ($max > 0 && strlen($s) > $max) {
        $s = substr($s, 0, $max);
        $d = strrpos($s, '-');
        if ($d !== false && $d > 10) $s = substr($s, 0, $d);
        $s = trim($s, '-');
    }
    return $s !== '' ? $s : 'video';
}
function absUrl($u, $base) {
    $u = trim((string)$u);
    if ($u === '') return '';
    if (preg_match('#^https?://#i', $u)) return $u;
    return $base . '/' . ltrim($u, '/');
}

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

$pages = [['/', '1.0', 'daily'], ['/dmca.html', '0.3', 'yearly'], ['/2257.html', '0.3', 'yearly'], ['/terms.html', '0.3', 'yearly'], ['/privacy.html', '0.3', 'yearly'], ['/removal.html', '0.3', 'yearly']];
foreach ($pages as $p) {
    $out .= '<url><loc>' . x($base . $p[0]) . '</loc><changefreq>' . $p[2] . '</changefreq><priority>' . $p[1] . "</priority></url>\n";
}

try {
    $res = $conn->query("SELECT id, title, slug, description, video_url, thumbnail_url, is_pro, created_at FROM videos ORDER BY id DESC LIMIT 50000");
    if ($res) {
        while ($v = $res->fetch_assoc()) {
            $slug = slugify(trim((string)$v['slug']) !== '' ? $v['slug'] : $v['title']);
            $loc = $base . '/watch/' . (int)$v['id'] . '/' . $slug;
            $ts = !empty($v['created_at']) ? strtotime($v['created_at']) : false;
            $last = $ts ? date('c', $ts) : '';
            $out .= '<url><loc>' . x($loc) . '</loc>';
            if ($last) $out .= '<lastmod>' . x($last) . '</lastmod>';
            $out .= '<changefreq>weekly</changefreq><priority>0.8</priority>';

            $thumb = absUrl($v['thumbnail_url'], $base);
            $desc = trim(preg_replace('/\s+/', ' ', (string)$v['description']));
            $vurl = absUrl($v['video_url'], $base);
            if (!(int)$v['is_pro'] && $thumb !== '' && $desc !== '' && $vurl !== '') {
                $out .= '<video:video><video:thumbnail_loc>' . x($thumb) . '</video:thumbnail_loc>';
                $out .= '<video:title>' . x(cut((string)$v['title'], 100)) . '</video:title>';
                $out .= '<video:description>' . x(cut($desc, 2000)) . '</video:description>';
                if (preg_match('/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/', $vurl, $m)) {
                    $out .= '<video:player_loc>' . x('https://www.youtube.com/embed/' . $m[1]) . '</video:player_loc>';
                } else {
                    $out .= '<video:content_loc>' . x($vurl) . '</video:content_loc>';
                }
                if ($last) $out .= '<video:publication_date>' . x($last) . '</video:publication_date>';
                $out .= '<video:family_friendly>no</video:family_friendly></video:video>';
            }
            $out .= "</url>\n";
        }
    }
} catch (Throwable $e) {
    // skip video rows on error; pages above still valid
}

$out .= "</urlset>\n";
ob_end_clean();
header('Content-Type: application/xml; charset=UTF-8');
echo $out;