<?php
declare(strict_types=1);
@ini_set('display_errors', '1');
@error_reporting(E_ALL);
@date_default_timezone_set('Asia/Jakarta');

require_once __DIR__ . '/db/config.php';

$eventId = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
$pdo = db();

$event = null;
if ($eventId) {
  $stmt = $pdo->prepare('SELECT id, event_type, message, meta, created_at FROM sahur_events WHERE id = :id');
  $stmt->execute([':id' => $eventId]);
  $event = $stmt->fetch();
}

$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? '';
$projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
?>
<!doctype html>
<html lang="id">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Detail Aktivitas Sahur</title>
<?php if ($projectDescription): ?>
  <meta name="description" content='<?= htmlspecialchars($projectDescription) ?>' />
  <meta property="og:description" content="<?= htmlspecialchars($projectDescription) ?>" />
  <meta property="twitter:description" content="<?= htmlspecialchars($projectDescription) ?>" />
<?php endif; ?>
<?php if ($projectImageUrl): ?>
  <meta property="og:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
  <meta property="twitter:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
<?php endif; ?>
  <meta name="robots" content="noindex, nofollow" />
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;600&display=swap" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="/assets/css/custom.css?v=<?= htmlspecialchars((string)time()) ?>" rel="stylesheet">
</head>
<body>
  <nav class="navbar navbar-expand-lg bg-white border-bottom">
    <div class="container">
      <a class="navbar-brand fw-semibold" href="/">Sahur Bot Control</a>
    </div>
  </nav>

  <main class="container py-5">
    <div class="d-flex align-items-center justify-content-between mb-4">
      <div>
        <p class="text-uppercase text-muted small mb-1">Detail Aktivitas</p>
        <h1 class="h4 fw-semibold mb-0">Log Sahur</h1>
      </div>
      <a class="btn btn-outline-secondary" href="/">Kembali</a>
    </div>

    <?php if (!$event): ?>
      <div class="alert alert-warning" role="alert">
        Data aktivitas tidak ditemukan. Pilih event dari daftar log.
      </div>
    <?php else: ?>
      <div class="card border-0 shadow-sm">
        <div class="card-body">
          <div class="row g-3">
            <div class="col-md-4">
              <p class="text-muted small mb-1">Tipe</p>
              <p class="fw-semibold mb-0 text-uppercase"><?= htmlspecialchars($event['event_type']) ?></p>
            </div>
            <div class="col-md-4">
              <p class="text-muted small mb-1">Waktu</p>
              <p class="fw-semibold mb-0"><?= htmlspecialchars($event['created_at']) ?> WIB</p>
            </div>
            <div class="col-md-4">
              <p class="text-muted small mb-1">ID Event</p>
              <p class="fw-semibold mb-0">#<?= htmlspecialchars((string)$event['id']) ?></p>
            </div>
            <div class="col-12">
              <p class="text-muted small mb-1">Pesan</p>
              <p class="mb-0"><?= htmlspecialchars($event['message']) ?></p>
            </div>
            <div class="col-12">
              <p class="text-muted small mb-1">Meta</p>
              <pre class="bg-light border rounded p-3 mb-0 small"><?= htmlspecialchars($event['meta'] ?: 'Tidak ada data tambahan.') ?></pre>
            </div>
          </div>
        </div>
      </div>
    <?php endif; ?>
  </main>

  <footer class="border-top py-4">
    <div class="container">
      <span class="text-muted small">Sahur Bot Dashboard (MVP)</span>
    </div>
  </footer>

  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
