<?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/service_status.php';

$summary = getServiceStatusSummary(false);

renderHeader('서비스 상태', 'service_status');
?>
<div class="service-status-page">
    <section class="service-status-hero status-<?php echo h(serviceStatusClass($summary['overall'])); ?>">
        <div>
            <span class="service-status-kicker"><i class="fa-solid fa-signal"></i> 서비스 상태</span>
            <h1 class="page-main-title"><?php echo h(serviceStatusLabel($summary['overall'])); ?></h1>
            <p class="page-subtitle">
                음악 재생, 스캔, 백업, PWA 상태를 민감정보 없이 요약해서 표시합니다.
            </p>
        </div>
        <span class="service-status-pill status-<?php echo h(serviceStatusClass($summary['overall'])); ?>">
            <?php echo h(serviceStatusLabel($summary['overall'])); ?>
        </span>
    </section>

    <?php if (!empty($summary['maintenance']['enabled'])): ?>
        <section class="service-maintenance-card">
            <div class="service-status-card-icon status-maintenance"><i class="fa-solid fa-screwdriver-wrench"></i></div>
            <div>
                <h2><?php echo h($summary['maintenance']['title']); ?></h2>
                <?php if ($summary['maintenance']['message'] !== ''): ?><p><?php echo nl2br(h($summary['maintenance']['message'])); ?></p><?php endif; ?>
            </div>
        </section>
    <?php endif; ?>

    <section class="service-status-grid">
        <?php foreach ($summary['items'] as $item): ?>
            <article class="service-status-card status-<?php echo h(serviceStatusClass($item['status'])); ?>">
                <div class="service-status-card-icon status-<?php echo h(serviceStatusClass($item['status'])); ?>">
                    <i class="fa-solid <?php
                    echo $item['key'] === 'playback' ? 'fa-play' :
                        ($item['key'] === 'scan' ? 'fa-rotate' :
                        ($item['key'] === 'cover' ? 'fa-image' :
                        ($item['key'] === 'lyrics' ? 'fa-music' :
                        ($item['key'] === 'pwa' ? 'fa-mobile-screen-button' :
                        ($item['key'] === 'backup' ? 'fa-database' : 'fa-triangle-exclamation')))));
                    ?>"></i>
                </div>
                <div>
                    <span><?php echo h(serviceStatusLabel($item['status'])); ?></span>
                    <h2><?php echo h($item['label']); ?></h2>
                    <p><?php echo h($item['public_detail']); ?></p>
                </div>
            </article>
        <?php endforeach; ?>
    </section>

    <section class="home-section service-status-help">
        <h2 class="section-title">이용 안내</h2>
        <p>재생이 되지 않거나 일부 기능이 불안정할 때는 이 화면에서 현재 상태를 먼저 확인할 수 있습니다. 상세 오류는 관리자 화면에서만 확인됩니다.</p>
    </section>
</div>
<?php renderFooter(); ?>
