<?php
session_start();
if (!isset($_SESSION['admin_id']) && $_SESSION['role'] != 'admin') {
    header("Location: ../login.php");
    exit();
}
include '../../include/config.php';

// Get key metrics
$total_users = $conn->query("SELECT COUNT(DISTINCT user_id) as cnt FROM hps_subscriptions")->fetch_assoc()['cnt'] ?? 0;
$active_subscriptions = $conn->query("SELECT COUNT(*) as cnt FROM hps_subscriptions WHERE status = 'active'")->fetch_assoc()['cnt'] ?? 0;
$total_invested = $conn->query("SELECT SUM(amount_invested) as total FROM hps_user_investments WHERE status = 'active'")->fetch_assoc()['total'] ?? 0;
$pending_withdrawals = $conn->query("SELECT SUM(amount) as total FROM withdrawal_requests WHERE status = 'pending'")->fetch_assoc()['total'] ?? 0;

$recent_users = $conn->query("
    SELECT u.*, 
           (SELECT COUNT(*) FROM hps_subscriptions WHERE user_id = u.patient_id) as has_subscription
    FROM patients u
    ORDER BY u.created_at DESC LIMIT 10
");

$page_title = 'HPS Admin Dashboard';
include '../../include/header.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HPS Admin Dashboard</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>
        body { background: #f0f2f5; font-family: 'Segoe UI', sans-serif; }
        .stat-card { background: white; border-radius: 20px; padding: 20px; text-align: center; border-bottom: 3px solid #d4a017; margin-bottom: 20px; }
        .stat-number { font-size: 1.8rem; font-weight: 800; color: #0a3d2f; }
        .nav-card { background: white; border-radius: 16px; padding: 20px; text-align: center; transition: 0.3s; cursor: pointer; height: 100%; }
        .nav-card:hover { transform: translateY(-5px); background: #d4a017; color: white; }
        .nav-card:hover i, .nav-card:hover h6 { color: white; }
        .nav-card i { font-size: 2rem; color: #d4a017; margin-bottom: 10px; }
    </style>
</head>
<body>

<div class="container mt-4">
    <h2><i class="fas fa-shield-alt me-2" style="color: #d4a017;"></i> HPS Admin Dashboard</h2>
    <p class="text-muted mb-4">Manage Health Protection Suite - Insurance & Investments</p>

    <!-- Stats -->
    <div class="row g-3 mb-4">
        <div class="col-md-3"><div class="stat-card"><i class="fas fa-users fa-2x mb-2"></i><div class="stat-number"><?= $total_users ?></div><p>Total HPS Users</p></div></div>
        <div class="col-md-3"><div class="stat-card"><i class="fas fa-check-circle fa-2x mb-2 text-success"></i><div class="stat-number"><?= $active_subscriptions ?></div><p>Active Subscriptions</p></div></div>
        <div class="col-md-3"><div class="stat-card"><i class="fas fa-chart-line fa-2x mb-2 text-info"></i><div class="stat-number">₦<?= number_format($total_invested, 2) ?></div><p>Total Invested</p></div></div>
        <div class="col-md-3"><div class="stat-card"><i class="fas fa-clock fa-2x mb-2 text-warning"></i><div class="stat-number">₦<?= number_format($pending_withdrawals, 2) ?></div><p>Pending Withdrawals</p></div></div>
    </div>

    <!-- Navigation Cards -->
    <div class="row g-3 mb-4">
        <div class="col-md-3"><div class="nav-card" onclick="location.href='payments.php'"><i class="fas fa-credit-card"></i><h6>Payments</h6><small>View all transactions</small></div></div>
        <div class="col-md-3"><div class="nav-card" onclick="location.href='withdrawals.php'"><i class="fas fa-hand-holding-usd"></i><h6>Withdrawals</h6><small>Manage requests</small></div></div>
        <div class="col-md-3"><div class="nav-card" onclick="location.href='subscriptions.php'"><i class="fas fa-file-contract"></i><h6>Subscriptions</h6><small>User plans</small></div></div>
        <div class="col-md-3"><div class="nav-card" onclick="location.href='investments.php'"><i class="fas fa-chart-line"></i><h6>Investments</h6><small>Portfolio management</small></div></div>
        <div class="col-md-3"><div class="nav-card" onclick="location.href='equipment.php'"><i class="fas fa-microscope"></i><h6>Equipment</h6><small>Investment assets</small></div></div>
        <div class="col-md-3"><div class="nav-card" onclick="location.href='users.php'"><i class="fas fa-users"></i><h6>HPS Users</h6><small>Member management</small></div></div>
        <div class="col-md-3"><div class="nav-card" onclick="location.href='reports.php'"><i class="fas fa-chart-bar"></i><h6>Reports</h6><small>Financial analytics</small></div></div>
    </div>

    <!-- Recent Users -->
    <div class="card shadow-sm border-0 rounded-4">
        <div class="card-header bg-white border-0 pt-3">
            <h5><i class="fas fa-user-plus me-2"></i> Recent HPS Users</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead><tr><th>ID</th><th>Name</th><th>Email</th><th>Joined</th><th>Status</th><th>Action</th></tr></thead>
                    <tbody>
                        <?php while($u = $recent_users->fetch_assoc()): ?>
                        <tr>
                            <td><?= $u['patient_id'] ?></td>
                            <td><?= htmlspecialchars($u['first_name'] . ' ' . $u['last_name']) ?></td>
                            <td><?= htmlspecialchars($u['email']) ?></td>
                            <td><?= date('M d, Y', strtotime($u['created_at'])) ?></td>
                            <td><?= $u['has_subscription'] ? '<span class="badge bg-success">Active</span>' : '<span class="badge bg-secondary">No Plan</span>' ?></td>
                            <td><a href="user_details.php?id=<?= $u['patient_id'] ?>" class="btn btn-sm btn-info">View</a></td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?php include '../../include/footer.php'; ?>