<?php
require_once 'includes/layout.php';
requireAdminLogin();

$data     = getDashboardStats();
$stats    = $data['stats'] ?? [];
$recent   = $data['recentPayments'] ?? [];
$movies   = $stats['movies']   ?? ['total'=>0,'published'=>0,'draft'=>0];
$users    = $stats['users']    ?? ['total'=>0,'active'=>0,'new_today'=>0];
$payments = $stats['payments'] ?? ['total_revenue'=>0,'today_revenue'=>0,'active_rentals'=>0];

function fmt(float $n): string { return 'K' . number_format($n, 0); }

ob_start();
?>
<div class="grid grid-4" style="margin-bottom:20px;">
    <div class="stat-card">
        <div class="stat-label">Total Movies</div>
        <div class="stat-value"><?= $movies['total'] ?></div>
        <div class="stat-sub"><?= $movies['published'] ?> published · <?= $movies['draft'] ?> draft</div>
    </div>
    <div class="stat-card">
        <div class="stat-label">Total Users</div>
        <div class="stat-value"><?= number_format($users['total']) ?></div>
        <div class="stat-sub"><span class="stat-up">+<?= $users['new_today'] ?></span> today</div>
    </div>
    <div class="stat-card">
        <div class="stat-label">Revenue Today</div>
        <div class="stat-value"><?= fmt((float)$payments['today_revenue']) ?></div>
        <div class="stat-sub"><?= $payments['active_rentals'] ?> active rentals</div>
    </div>
    <div class="stat-card">
        <div class="stat-label">Total Revenue</div>
        <div class="stat-value"><?= fmt((float)$payments['total_revenue']) ?></div>
        <div class="stat-sub">All completed payments</div>
    </div>
</div>

<div class="grid grid-2">
    <div class="card">
        <div class="card-header">
            <span class="card-title">Recent Payments</span>
            <a href="payments.php" class="btn btn-surface" style="font-size:12px;padding:5px 10px;">View All</a>
        </div>
        <div class="table-wrap">
            <table>
                <thead><tr><th>User</th><th>Movie</th><th>Method</th><th>Amount</th><th>Status</th></tr></thead>
                <tbody>
                <?php if (empty($recent)): ?>
                    <tr><td colspan="5" style="text-align:center;color:var(--text-muted);padding:20px;">No payments yet</td></tr>
                <?php else: foreach ($recent as $p):
                    $badge = match($p['status']) {
                        'completed' => 'badge-green',
                        'pending'   => 'badge-orange',
                        default     => 'badge-red',
                    };
                ?>
                <tr>
                    <td style="font-weight:600;"><?= htmlspecialchars($p['user_name'] ?? '—') ?></td>
                    <td style="color:var(--text-sec);font-size:12px;"><?= htmlspecialchars($p['movie_title'] ?? '—') ?></td>
                    <td style="font-size:12px;"><?= htmlspecialchars($p['method'] ?? '—') ?></td>
                    <td style="font-weight:700;color:var(--orange);">K<?= number_format((float)($p['amount'] ?? 0)) ?></td>
                    <td><span class="badge <?= $badge ?>"><?= $p['status'] ?></span></td>
                </tr>
                <?php endforeach; endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="card">
        <div class="card-header">
            <span class="card-title">Quick Actions</span>
        </div>
        <div class="card-body" style="display:flex;flex-direction:column;gap:10px;">
            <a href="upload.php" class="btn btn-orange" style="justify-content:center;">+ Upload New Movie</a>
            <a href="movies.php" class="btn btn-surface" style="justify-content:center;">Manage Movies</a>
            <a href="users.php" class="btn btn-surface" style="justify-content:center;">Manage Users</a>
            <a href="payments.php" class="btn btn-surface" style="justify-content:center;">View Payments</a>
            <a href="categories.php" class="btn btn-surface" style="justify-content:center;">Manage Categories</a>
        </div>
    </div>
</div>
<?php
$content = ob_get_clean();
renderLayout('Dashboard', 'dashboard', $content);
