<?php
require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/includes/auth.php';
require_once __DIR__ . '/includes/layout.php';
require_once __DIR__ . '/includes/patch_notes.php';

try {
    $patchNotes = fetchVisiblePatchNotes(12);
} catch (Throwable $e) {
    $patchNotes = [];
    if (function_exists('writeAppLog')) {
        writeAppLog('warning', 'patch_note_error', 'Patch note load failed', [
            'error' => limitLogText($e->getMessage(), 300),
        ]);
    }
}

renderHeader('최근 업데이트', 'updates');
?>
<div class="patch-notes-page">
    <section class="patch-notes-hero">
        <div>
            <span class="patch-eyebrow"><i class="fa-solid fa-clock-rotate-left"></i> 최근 업데이트</span>
            <h1 class="page-main-title">서비스 개선 내역</h1>
            <p class="page-subtitle">최근 적용된 기능 추가, 버그 수정, 안정화 작업을 한눈에 확인할 수 있습니다.</p>
        </div>
        <?php if (!empty($patchNotes)): ?>
            <span class="patch-latest-badge">최신 <?php echo number_format(count($patchNotes)); ?>개</span>
        <?php endif; ?>
    </section>

    <section class="home-section patch-notes-list-section">
        <?php if (empty($patchNotes)): ?>
            <div class="empty-state">
                <i class="fa-regular fa-note-sticky"></i>
                <strong>표시할 업데이트 내역이 없습니다.</strong>
                <p>관리자 화면에서 패치노트를 등록하면 이곳에 표시됩니다.</p>
            </div>
        <?php else: ?>
            <div class="patch-note-timeline">
                <?php foreach ($patchNotes as $index => $note): ?>
                    <?php
                    $body = trim((string)($note['body'] ?? ''));
                    $long = function_exists('mb_strlen') ? mb_strlen($body, 'UTF-8') > 180 : strlen($body) > 180;
                    $date = $note['published_at'] ?: $note['created_at'];
                    ?>
                    <article class="patch-note-card patch-type-<?php echo h(patchNoteTypeClass($note['note_type'] ?? 'stability')); ?>">
                        <div class="patch-note-marker" aria-hidden="true"></div>
                        <div class="patch-note-card-body">
                            <div class="patch-note-meta">
                                <span class="patch-type-badge"><?php echo h(patchNoteTypeLabel($note['note_type'] ?? 'stability')); ?></span>
                                <?php if (!empty($note['release_version'])): ?><span class="patch-version"><?php echo h($note['release_version']); ?></span><?php endif; ?>
                                <?php if ($index === 0): ?><span class="patch-new-badge">NEW</span><?php endif; ?>
                                <time datetime="<?php echo h($date); ?>"><?php echo h(date('Y-m-d', strtotime((string)$date))); ?></time>
                            </div>
                            <h2><?php echo h($note['title']); ?></h2>
                            <?php if ($body !== ''): ?>
                                <?php if ($long): ?>
                                    <details class="patch-note-details">
                                        <summary><?php echo h(function_exists('mb_substr') ? mb_substr($body, 0, 180, 'UTF-8') . '...' : substr($body, 0, 180) . '...'); ?></summary>
                                        <p><?php echo nl2br(h($body)); ?></p>
                                    </details>
                                <?php else: ?>
                                    <p><?php echo nl2br(h($body)); ?></p>
                                <?php endif; ?>
                            <?php endif; ?>
                        </div>
                    </article>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </section>
</div>
<?php renderFooter(); ?>
