<?php require_once __DIR__ . '/includes/admin_bootstrap.php';
ugfx_require_permission('dashboard');
$adminTitle='Dashboard';
$news = ugfx_read_json($ugfx['storage']['news'], []);
$events = ugfx_read_json($ugfx['storage']['events'], []);
$requests = ugfx_read_json($ugfx['storage']['requests'], []);
$messages = ugfx_chat_messages(0);
$applications = ugfx_read_json($ugfx['storage']['applications'], []);
$pages = ugfx_get_pages(false);
$friends = ugfx_get_friends(999);
$djs = ugfx_read_json($ugfx['storage']['djs'], []);
$contact = ugfx_read_json($ugfx['storage']['contact'], []);
$charts = ugfx_top50_all();
$chartStats = ugfx_top50_stats();
$chatStatus = ugfx_chat_public_status();

$requestCounts = ['all' => count($requests), 'new' => 0, 'done' => 0];
foreach ($requests as $request) {
    $status = strtolower(trim((string)($request['status'] ?? 'new')) ?: 'new');
    if (!isset($requestCounts[$status])) {
        $requestCounts[$status] = 0;
    }
    $requestCounts[$status]++;
}

$publishedNews = count(array_filter($news, fn($item) => !isset($item['published']) || !empty($item['published'])));
$publishedEvents = count(array_filter($events, fn($item) => !isset($item['published']) || !empty($item['published'])));
$publishedPages = count(array_filter($pages, fn($item) => !isset($item['published']) || !empty($item['published'])));

include __DIR__ . '/includes/admin_header.php';
?>
<div class="row g-4 mb-4">
  <div class="col-md-3"><div class="ugfx-card p-4"><div class="ugfx-micro-label">NEWS</div><h2><?= count($news) ?></h2><a class="btn ugfx-btn-outline btn-sm" href="news.php">Manage</a></div></div>
  <div class="col-md-3"><div class="ugfx-card p-4"><div class="ugfx-micro-label">EVENTS</div><h2><?= count($events) ?></h2><a class="btn ugfx-btn-outline btn-sm" href="events.php">Manage</a></div></div>
  <div class="col-md-3"><div class="ugfx-card p-4"><div class="ugfx-micro-label">PAGES</div><h2><?= count($pages) ?></h2><a class="btn ugfx-btn-outline btn-sm" href="pages.php">Manage</a></div></div>
  <div class="col-md-3"><div class="ugfx-card p-4"><div class="ugfx-micro-label">CHAT</div><h2><?= count($messages) ?></h2><div class="mb-2"><span class="ugfx-chip <?= $chatStatus['enabled'] ? 'ugfx-chip-blue' : '' ?>"><?= $chatStatus['enabled'] ? 'Public chat live' : 'Public chat off' ?></span></div><a class="btn ugfx-btn-outline btn-sm" href="chat.php">Moderate</a></div></div>
  <div class="col-md-3"><div class="ugfx-card p-4"><div class="ugfx-micro-label">FRIENDS</div><h2><?= count($friends) ?></h2><a class="btn ugfx-btn-outline btn-sm" href="friends.php">Manage</a></div></div>
  <div class="col-md-3"><div class="ugfx-card p-4"><div class="ugfx-micro-label">DJS / TEAM</div><h2><?= count($djs) ?></h2><a class="btn ugfx-btn-outline btn-sm" href="djs.php">Manage</a></div></div>
  <div class="col-md-3"><div class="ugfx-card p-4"><div class="ugfx-micro-label">CHARTS</div><h2><?= count($charts) ?></h2><div class="mb-2"><span class="ugfx-chip">Top plays: <?= (int)($chartStats['total_plays'] ?? 0) ?></span></div><a class="btn ugfx-btn-outline btn-sm" href="charts.php">Manage</a></div></div>
  <div class="col-md-3"><div class="ugfx-card p-4"><div class="ugfx-micro-label">REQUESTS</div><h2><?= count($requests) ?></h2><a class="btn ugfx-btn-outline btn-sm" href="requests.php">View queue</a></div></div>
  <div class="col-md-3"><div class="ugfx-card p-4"><div class="ugfx-micro-label">CONTACT</div><h2><?= count($contact) ?></h2><a class="btn ugfx-btn-outline btn-sm" href="contact.php">Review</a></div></div>
  <div class="col-md-4"><div class="ugfx-card p-4"><div class="ugfx-micro-label">APPLICATIONS</div><h2><?= count($applications) ?></h2><a class="btn ugfx-btn-outline btn-sm" href="applications.php">Review</a></div></div>
</div>

<div class="row g-4">
  <div class="col-lg-6">
    <div class="ugfx-card p-4 h-100">
      <div class="ugfx-micro-label">SITE STATUS</div>
      <h3>Live ready overview</h3>
      <div class="ugfx-admin-list">
        <div class="ugfx-admin-item">Public chat: <strong><?= $chatStatus['enabled'] ? 'On' : 'Off' ?></strong></div>
        <div class="ugfx-admin-item">Published news items: <strong><?= (int)$publishedNews ?></strong></div>
        <div class="ugfx-admin-item">Published events: <strong><?= (int)$publishedEvents ?></strong></div>
        <div class="ugfx-admin-item">Published custom pages: <strong><?= (int)$publishedPages ?></strong></div>
        <div class="ugfx-admin-item">Open song requests: <strong><?= (int)($requestCounts['new'] ?? 0) ?></strong></div>
        <div class="ugfx-admin-item">Chart entries ready: <strong><?= (int)count($charts) ?></strong></div>
      </div>
    </div>
  </div>
  <div class="col-lg-6">
    <div class="ugfx-card p-4 h-100">
      <div class="ugfx-micro-label">QUICK ACTIONS</div>
      <h3>Most used admin tools</h3>
      <div class="d-flex gap-2 flex-wrap">
        <a class="btn ugfx-btn-primary btn-sm" href="news.php">News</a>
        <a class="btn ugfx-btn-outline btn-sm" href="events.php">Events</a>
        <a class="btn ugfx-btn-outline btn-sm" href="schedule.php">Schedule</a>
        <a class="btn ugfx-btn-outline btn-sm" href="charts.php">Charts</a>
        <a class="btn ugfx-btn-outline btn-sm" href="requests.php">Requests</a>
        <a class="btn ugfx-btn-outline btn-sm" href="chat.php">Chat</a>
        <a class="btn ugfx-btn-outline btn-sm" href="djs.php">DJs</a>
        <a class="btn ugfx-btn-outline btn-sm" href="friends.php">Friends</a>
        <a class="btn ugfx-btn-outline btn-sm" href="backup.php">Backup</a>
      </div>
      <div class="ugfx-admin-list mt-3">
        <div class="ugfx-admin-item">Only live admin shortcuts are shown here now.</div>
        <div class="ugfx-admin-item">Build notes, upgrade notes, and dev checklist blocks have been removed.</div>
      </div>
    </div>
  </div>
</div>
<?php include __DIR__ . '/includes/admin_footer.php'; ?>
