<?php
require_once __DIR__ . '/../functions.php';
ensure_role(['Admin', 'Manager']);

$totalUsers = count_rows('users');
$totalPortfolio = count_rows('portfolio_projects');
$totalBlog = count_rows('blog_posts');
$totalQuotes = count_rows('quote_requests');

$activity = pdo()->query('SELECT al.action, al.meta, al.created_at, u.name AS user_name FROM activity_logs al LEFT JOIN users u ON al.user_id = u.id ORDER BY al.created_at DESC LIMIT 8')->fetchAll();
?>
<?php include __DIR__ . '/includes/header.php'; ?>
    <div class="dash-grid">
        <article class="card gradient-card red-blue">
            <h3>Total Users</h3>
            <p><?= $totalUsers ?></p>
        </article>
        <article class="card gradient-card blue-red">
            <h3>Portfolio Projects</h3>
            <p><?= $totalPortfolio ?></p>
        </article>
        <article class="card gradient-card red-blue">
            <h3>Blog Posts</h3>
            <p><?= $totalBlog ?></p>
        </article>
        <article class="card gradient-card blue-red">
            <h3>Quote Requests</h3>
            <p><?= $totalQuotes ?></p>
        </article>
    </div>
    <section class="panel">
        <header><h2>Recent Activity</h2></header>
        <div class="table-scroll">
            <table>
                <thead><tr><th>User</th><th>Action</th><th>Meta</th><th>When</th></tr></thead>
                <tbody>
                <?php foreach ($activity as $item): ?>
                    <tr>
                        <td><?= clean($item['user_name'] ?? 'System') ?></td>
                        <td><?= clean($item['action']) ?></td>
                        <td><?= clean($item['meta']) ?></td>
                        <td><?= clean($item['created_at']) ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </section>
<?php include __DIR__ . '/includes/footer.php'; ?>
