<?php
declare(strict_types=1);

function admin_count(string $table): int
{
    try {
        return (int)db()->query('SELECT COUNT(*) FROM ' . $table)->fetchColumn();
    } catch (Throwable $e) {
        return 0;
    }
}

require_once __DIR__ . '/includes/bootstrap.php';
require_admin();

$newEnquiries = 0;
try {
    $newEnquiries = (int)db()->query("SELECT COUNT(*) FROM enquiries WHERE status = 'new'")->fetchColumn();
} catch (Throwable $e) {
}

$recent = [];
try {
    $recent = db()->query('SELECT id, name, email, created_at, status FROM enquiries ORDER BY created_at DESC LIMIT 6')->fetchAll();
} catch (Throwable $e) {
}

$page = 'dash';
$pageTitle = 'Dashboard';
$pageSubtitle = 'Overview of your website content, hero banners, and contact leads.';
$breadcrumbs = [['label' => 'Dashboard', 'href' => null]];
require __DIR__ . '/includes/layout-header.php';
?>
<div class="row g-3 mb-4">
  <div class="col-sm-6 col-xl-3">
    <div class="admin-card rounded p-3 p-lg-4 h-100">
      <div class="d-flex justify-content-between align-items-start">
        <div>
          <p class="text-uppercase text-muted mb-1 admin-dash-stat-label">Services</p>
          <p class="fs-2 fw-bold text-dark mb-0 mb-1"><?= admin_count('services') ?></p>
        </div>
        <span class="rounded-3 p-2 admin-dash-icon"><i class="bi bi-grid-1x2 fs-5"></i></span>
      </div>
      <a href="services.php" class="stretched-link text-decoration-none small fw-semibold text-secondary">Manage services <i class="bi bi-arrow-right small"></i></a>
    </div>
  </div>
  <div class="col-sm-6 col-xl-3">
    <div class="admin-card rounded p-3 p-lg-4 h-100">
      <div class="d-flex justify-content-between align-items-start">
        <div>
          <p class="text-uppercase text-muted mb-1 admin-dash-stat-label">Projects</p>
          <p class="fs-2 fw-bold text-dark mb-0 mb-1"><?= admin_count('projects') ?></p>
        </div>
        <span class="rounded-3 p-2 admin-dash-icon"><i class="bi bi-folder2-open fs-5"></i></span>
      </div>
      <a href="projects.php" class="stretched-link text-decoration-none small fw-semibold text-secondary">Manage portfolio <i class="bi bi-arrow-right small"></i></a>
    </div>
  </div>
  <div class="col-sm-6 col-xl-3">
    <div class="admin-card rounded p-3 p-lg-4 h-100">
      <div class="d-flex justify-content-between align-items-start">
        <div>
          <p class="text-uppercase text-muted mb-1 admin-dash-stat-label">Clients</p>
          <p class="fs-2 fw-bold text-dark mb-0 mb-1"><?= admin_count('clients') ?></p>
        </div>
        <span class="rounded-3 p-2 admin-dash-icon"><i class="bi bi-building fs-5"></i></span>
      </div>
      <a href="clients.php" class="stretched-link text-decoration-none small fw-semibold text-secondary">Manage clients <i class="bi bi-arrow-right small"></i></a>
    </div>
  </div>
  <div class="col-sm-6 col-xl-3">
    <div class="admin-card rounded p-3 p-lg-4 h-100">
      <div class="d-flex justify-content-between align-items-start">
        <div>
          <p class="text-uppercase text-muted mb-1 admin-dash-stat-label">Team</p>
          <p class="fs-2 fw-bold text-dark mb-0 mb-1"><?= admin_count('team_members') ?></p>
        </div>
        <span class="rounded-3 p-2 admin-dash-icon"><i class="bi bi-people fs-5"></i></span>
      </div>
      <a href="team.php" class="stretched-link text-decoration-none small fw-semibold text-secondary">Manage team <i class="bi bi-arrow-right small"></i></a>
    </div>
  </div>
</div>

<div class="row g-3">
  <div class="col-lg-4">
    <div class="admin-card admin-enquiry-spotlight rounded h-100">
      <div class="card-body p-4">
        <p class="text-uppercase mb-1 admin-dash-stat-label admin-dash-stat-label--emphasis">New enquiries</p>
        <p class="fs-2 fw-bold mb-2 text-dark"><?= $newEnquiries ?></p>
        <p class="text-muted small mb-3">Unread contact form submissions.</p>
        <a href="enquiries.php" class="btn btn-primary btn-sm">Open inbox</a>
      </div>
    </div>
  </div>
  <div class="col-lg-4">
    <div class="admin-card rounded p-4 h-100">
      <p class="text-uppercase text-muted mb-1 admin-dash-stat-label">Hero banners</p>
      <p class="fs-2 fw-bold text-dark mb-2"><?= admin_count('banners') ?></p>
      <p class="text-muted small mb-3">Homepage hero slides.</p>
      <a href="banners.php" class="btn btn-outline-secondary btn-sm">Edit banners</a>
    </div>
  </div>
  <div class="col-lg-4">
    <div class="admin-card rounded p-4 h-100">
      <p class="text-uppercase text-muted mb-1 admin-dash-stat-label">FAQ entries</p>
      <p class="fs-2 fw-bold text-dark mb-2"><?= admin_count('faqs') ?></p>
      <p class="text-muted small mb-3">Public FAQ page.</p>
      <a href="faqs.php" class="btn btn-outline-secondary btn-sm">Manage FAQ</a>
    </div>
  </div>
</div>

<?php if ($recent): ?>
<div class="admin-card rounded mt-4">
  <div class="card-header d-flex justify-content-between align-items-center py-3">
    <span><i class="bi bi-clock-history me-2"></i>Recent enquiries</span>
    <a href="enquiries.php" class="btn btn-sm btn-outline-primary">View all</a>
  </div>
  <div class="table-responsive">
    <table class="table table-hover admin-table mb-0">
      <thead><tr><th>ID</th><th>Date</th><th>Name</th><th>Email</th><th>Status</th><th></th></tr></thead>
      <tbody>
        <?php foreach ($recent as $r): ?>
          <tr>
            <td><?= (int)$r['id'] ?></td>
            <td class="text-muted small"><?= h((string)$r['created_at']) ?></td>
            <td><?= h((string)$r['name']) ?></td>
            <td><?= h((string)$r['email']) ?></td>
            <td><span class="badge bg-<?= $r['status'] === 'new' ? 'primary' : 'secondary' ?>"><?= h((string)$r['status']) ?></span></td>
            <td><a href="enquiry-detail.php?id=<?= (int)$r['id'] ?>" class="btn btn-sm btn-outline-primary">View</a></td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
</div>
<?php endif; ?>

<?php require __DIR__ . '/includes/layout-footer.php'; ?>
