<?php
require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/includes/db.php';
require_once dirname(__DIR__) . '/includes/functions.php';
require_once dirname(__DIR__) . '/includes/auth.php';
require_once dirname(__DIR__) . '/includes/layout.php';
requirePermission('log_view');

$id = (int)($_GET['id'] ?? 0);
$stmt = getDBConnection()->prepare('SELECT * FROM app_logs WHERE id = :id');
$stmt->execute([':id' => $id]);
$log = $stmt->fetch();

renderHeader('로그 상세', 'logs');
?>
<div class="admin-dashboard logs-page">
    <div class="page-title-section">
        <h1 class="page-main-title">로그 상세</h1>
        <p class="page-subtitle">로그 context는 HTML escape 후 표시됩니다.</p>
    </div>
    <?php if (!$log): ?>
        <section class="home-section"><p class="empty-text">로그를 찾을 수 없습니다.</p></section>
    <?php else: ?>
        <?php
        $context = (string)($log['context'] ?? '');
        $decoded = json_decode($context, true);
        $contextDisplay = is_array($decoded) ? json_encode($decoded, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) : $context;
        ?>
        <section class="settings-card log-detail-card">
            <dl class="detail-list">
                <dt>시간</dt><dd><?php echo h($log['created_at']); ?></dd>
                <dt>레벨</dt><dd><?php echo h($log['log_level']); ?></dd>
                <dt>타입</dt><dd><?php echo h($log['log_type']); ?></dd>
                <dt>메시지</dt><dd><?php echo h($log['message']); ?></dd>
                <dt>파일</dt><dd><?php echo h($log['file_path'] ?: '-'); ?></dd>
                <dt>IP</dt><dd><?php echo h($log['ip_address'] ?: '-'); ?></dd>
                <dt>User Agent</dt><dd><?php echo h($log['user_agent'] ?: '-'); ?></dd>
            </dl>
            <h2 class="section-title">Context</h2>
            <pre class="log-context"><?php echo h($contextDisplay ?: '-'); ?></pre>
            <a class="btn btn-secondary" href="logs.php">목록으로</a>
        </section>
    <?php endif; ?>
</div>
<?php renderFooter(); ?>
