<?php
require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/../includes/layout.php';

$user = requireAuth();

$stats = [
    'projects'  => fetchOne("SELECT COUNT(*) AS c FROM projects WHERE user_id = ?", [$user['id']])['c'],
    'clients'   => fetchOne("SELECT COUNT(*) AS c FROM clients  WHERE user_id = ?", [$user['id']])['c'],
    'active'    => fetchOne("SELECT COUNT(*) AS c FROM projects WHERE user_id = ? AND status = 'active'", [$user['id']])['c'],
    'completed' => fetchOne("SELECT COUNT(*) AS c FROM projects WHERE user_id = ? AND status = 'completed'", [$user['id']])['c'],
];

$recent = fetchAll(
    "SELECT p.*, c.name AS client_name FROM projects p
     JOIN clients c ON c.id = p.client_id
     WHERE p.user_id = ?
     ORDER BY p.updated_at DESC LIMIT 8",
    [$user['id']]
);

renderHead('Dashboard');
renderNav($user);
renderFlash();
?>

<div class="page-header">
  <div>
    <h1>Good day, <?= e(explode(' ', $user['name'])[0]) ?> 👋</h1>
    <p class="subtitle">Here's what's happening across your projects.</p>
  </div>
  <a href="<?= APP_URL ?>/project-new.php" class="btn btn-primary">
    <svg viewBox="0 0 24 24"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
    New Project
  </a>
</div>

<div class="stats-grid">
  <div class="stat-card">
    <div class="stat-label">Total Projects</div>
    <div class="stat-value"><?= $stats['projects'] ?></div>
  </div>
  <div class="stat-card">
    <div class="stat-label">Clients</div>
    <div class="stat-value"><?= $stats['clients'] ?></div>
  </div>
  <div class="stat-card">
    <div class="stat-label">Active</div>
    <div class="stat-value" style="color:var(--green)"><?= $stats['active'] ?></div>
  </div>
  <div class="stat-card">
    <div class="stat-label">Completed</div>
    <div class="stat-value" style="color:var(--text2)"><?= $stats['completed'] ?></div>
  </div>
</div>

<div class="card">
  <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:16px;">
    <h2 class="card-title" style="margin:0">Recent Projects</h2>
    <a href="<?= APP_URL ?>/projects.php" class="btn btn-ghost btn-sm">View all</a>
  </div>
  <?php if ($recent): ?>
  <div class="table-wrap">
    <table>
      <thead>
        <tr>
          <th>Project</th>
          <th>Client</th>
          <th>Status</th>
          <th>Last Updated</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($recent as $p): ?>
        <tr>
          <td><strong><?= e($p['title']) ?></strong></td>
          <td><?= e($p['client_name']) ?></td>
          <td><?= statusBadge($p['status']) ?></td>
          <td style="color:var(--text2)"><?= formatDate($p['updated_at']) ?></td>
          <td><a href="<?= APP_URL ?>/project.php?id=<?= $p['id'] ?>" class="btn btn-ghost btn-sm">Open →</a></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>
  <?php else: ?>
  <div class="empty-state">
    <div class="big-icon">📋</div>
    <h3>No projects yet</h3>
    <p>Create your first project to get started.</p>
    <a href="<?= APP_URL ?>/project-new.php" class="btn btn-primary" style="margin-top:16px;">Create Project</a>
  </div>
  <?php endif; ?>
</div>

<?php renderFooter(); ?>
