<?php
// 방문자용 HTML 사이트맵 (URL 목록은 include/sitemap_allowlist.php 화이트리스트와 동일)
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/error.log');

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

$baseDir = __DIR__;
$httpsOn = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
    || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https');
$baseUrl = ($httpsOn ? 'https' : 'http') . '://' . ($_SERVER['HTTP_HOST'] ?? '');

$allow = require $baseDir . '/include/sitemap_allowlist.php';
$publicFiles = [];
if (is_array($allow)) {
    foreach ($allow as $row) {
        if (!is_array($row) || empty($row['loc_path']) || empty($row['file'])) {
            continue;
        }
        $abs = $baseDir . '/' . ltrim(str_replace('\\', '/', (string) $row['file']), '/');
        if (!is_file($abs)) {
            continue;
        }
        $publicFiles[] = (string) $row['loc_path'];
    }
}
$publicFiles = array_values(array_unique($publicFiles));
sort($publicFiles, SORT_STRING | SORT_FLAG_CASE);

// 디렉토리별 그룹화
$groups = [];
foreach ($publicFiles as $path) {
    if ($path === '/') {
        $dir = '/';
    } else {
        $dir = str_replace('\\', '/', dirname($path));
        $dir = trim($dir, '/');
        if ($dir === '' || $dir === '.') {
            $dir = '/';
        }
    }
    $groups[$dir][] = $path;
}
ksort($groups, SORT_STRING | SORT_FLAG_CASE);

?>
<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>사이트맵</title>
    <style>
        body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; margin: 0; background:#fff; color:#1f2f35; }
        .container { max-width: 1100px; margin: 0 auto; padding: 24px 16px; }
        h1 { margin: 0 0 8px 0; font-size: 1.8rem; }
        .desc { color:#6a7b80; margin-bottom: 18px; }
        .group { margin: 18px 0 24px 0; }
        .group h2 { font-size: 1.05rem; color:#6a7b80; margin: 0 0 10px 0; }
        .links { display: grid; grid-template-columns: repeat(2, minmax(220px, 1fr)); gap: 8px 14px; }
        @media (max-width: 640px) { .links { grid-template-columns: 1fr; } }
        a { color:#0e5b6c; text-decoration: none; }
        a:hover { text-decoration: underline; }
        .muted { color:#9aa9ae; font-size: 0.9rem; }
        .badge { display:inline-block; background:#eef7f9; color:#0e5b6c; border:1px solid #cfe7ec; padding:2px 6px; border-radius:8px; font-size: 12px; margin-left: 6px; }
    </style>
</head>
<body>
    <?php include './header.php'; ?>
    <?php include './nav.php'; ?>
    <div class="container">
        <h1>사이트맵</h1>
        <div class="desc">검색용 XML 사이트맵과 동일한 공개 페이지만 나열합니다. (<a href="/sitemap.xml">sitemap.xml</a>)</div>

        <?php foreach ($groups as $dir => $paths): ?>
            <div class="group">
                <h2><?= $dir === '/' ? '루트' : htmlspecialchars('/' . $dir) ?></h2>
                <div class="links">
                    <?php foreach ($paths as $p): ?>
                        <?php
                            $url = rtrim($baseUrl, '/') . $p;
                            $label = $p;
                            // index 라벨 정리
                            if ($p === '/') $label = '/ (홈)';
                        ?>
                        <div><a href="<?= htmlspecialchars($url) ?>"><?= htmlspecialchars($label) ?></a></div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endforeach; ?>

        <?php if (empty($publicFiles)): ?>
            <p class="muted">표시할 공개 페이지가 없습니다.</p>
        <?php endif; ?>
    </div>
    <?php include './footer.php'; ?>
</body>
<?php /* XML 사이트맵은 /sitemap.xml (sitemap.xml.php) 사용. 본 페이지는 방문자용 HTML 목록만 출력합니다. */ ?>

