<?php

require_once __DIR__ . '/../app/config/config.php';
require_once __DIR__ . '/../app/config/database.php';
require_once __DIR__ . '/../app/helpers/functions.php';
require_once __DIR__ . '/../app/helpers/auth.php';
require_once __DIR__ . '/../app/helpers/settings.php';
require_once __DIR__ . '/../app/helpers/activity.php';

require_admin();

$pageTitle = 'Dashboard';

$totalProducts = $pdo->query("SELECT COUNT(*) FROM products")->fetchColumn();
$totalOrders = $pdo->query("SELECT COUNT(*) FROM orders")->fetchColumn();
$totalCustomers = $pdo->query("SELECT COUNT(*) FROM customers")->fetchColumn();
$totalSales = $pdo->query("SELECT COALESCE(SUM(total_amount), 0) FROM orders WHERE payment_status = 'paid'")->fetchColumn();

$recentOrders = $pdo->query("
    SELECT order_number, customer_email, total_amount, currency_code, order_status, created_at
    FROM orders
    ORDER BY id DESC
    LIMIT 5
")->fetchAll();

$recentActivities = $pdo->query("
    SELECT al.*,
           au.full_name,
           au.email
    FROM admin_activity_logs al
    LEFT JOIN admin_users au ON al.admin_id = au.id
    ORDER BY al.id DESC
    LIMIT 8
")->fetchAll();

require_once __DIR__ . '/includes/header.php';
require_once __DIR__ . '/includes/sidebar.php';
?>

<main class="admin-main">
    <div class="admin-topbar">
        <h1>Dashboard</h1>
        <div class="admin-user">Logged in as <?= e(admin()['full_name']); ?></div>
    </div>

    <section class="admin-cards">
        <div class="admin-card">
            <span>Total Sales</span>
            <strong><?= e(format_price($totalSales)); ?></strong>
        </div>

        <div class="admin-card">
            <span>Orders</span>
            <strong><?= e($totalOrders); ?></strong>
        </div>

        <div class="admin-card">
            <span>Products</span>
            <strong><?= e($totalProducts); ?></strong>
        </div>

        <div class="admin-card">
            <span>Customers</span>
            <strong><?= e($totalCustomers); ?></strong>
        </div>
    </section>

    <section class="admin-panel">
        <h2>Recent Orders</h2>

        <table class="admin-table">
            <thead>
                <tr>
                    <th>Order</th>
                    <th>Customer</th>
                    <th>Total</th>
                    <th>Status</th>
                    <th>Date</th>
                </tr>
            </thead>

            <tbody>
                <?php if (!$recentOrders): ?>
                    <tr>
                        <td colspan="5">No orders yet.</td>
                    </tr>
                <?php endif; ?>

                <?php foreach ($recentOrders as $order): ?>
                    <tr>
                        <td><?= e($order['order_number']); ?></td>
                        <td><?= e($order['customer_email']); ?></td>
                        <td><?= e(format_price($order['total_amount'], $order['currency_code'])); ?></td>
                        <td><?= e(ucfirst($order['order_status'])); ?></td>
                        <td><?= e(date('d M Y', strtotime($order['created_at']))); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        </section>

    <section class="admin-panel" style="margin-top:24px;">
        <div style="display:flex; justify-content:space-between; align-items:center; gap:16px; margin-bottom:16px;">
            <h2 style="margin:0;">Recent Activity</h2>

        <a href="<?= e(ADMIN_URL); ?>/activity-logs.php" class="admin-btn admin-btn-light">
            View All
        </a>
    </div>

    <table class="admin-table">
        <thead>
            <tr>
                <th>Admin</th>
                <th>Action</th>
                <th>Description</th>
                <th>Date</th>
            </tr>
        </thead>

        <tbody>
            <?php if (!$recentActivities): ?>
                <tr>
                    <td colspan="4">No recent activity yet.</td>
                </tr>
            <?php endif; ?>

            <?php foreach ($recentActivities as $activity): ?>
                <tr>
                    <td>
                        <?php if (!empty($activity['admin_id'])): ?>
                            <strong><?= e($activity['full_name'] ?? 'Admin'); ?></strong><br>
                            <small><?= e($activity['email'] ?? '-'); ?></small>
                        <?php else: ?>
                            System / Unknown
                        <?php endif; ?>
                    </td>

                    <td>
                        <strong><?= e(str_replace('_', ' ', ucfirst($activity['action']))); ?></strong>
                    </td>

                    <td><?= e($activity['description'] ?? '-'); ?></td>

                    <td><?= e(date('d M Y, h:i A', strtotime($activity['created_at']))); ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</section>
</main>
<?php require_once __DIR__ . '/includes/footer.php'; ?>