<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/middleware.php';
require_once __DIR__ . '/../config/database.php';
require_admin();
include __DIR__ . '/../includes/header.php';
?>
<style>
/* Dashboard Specific Styles */
.dashboard-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 40px; gap: 20px; flex-wrap: wrap; }
.dashboard-title { margin: 0; font-size: 32px; font-weight: 800; color: var(--text); letter-spacing: -0.02em; }
.dashboard-subtitle { margin: 4px 0 0; color: var(--muted); font-size: 16px; }

/* KPI Cards */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 24px; margin-bottom: 48px; }
.kpi-card { background: #fff; border: 1px solid var(--border); border-radius: 24px; padding: 24px; display: flex; align-items: center; gap: 20px; transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1); box-shadow: var(--shadow-sm); position: relative; overflow: hidden; }
.kpi-card:hover { border-color: var(--primary); transform: translateY(-6px); box-shadow: var(--shadow); }
.kpi-icon { width: 56px; height: 56px; border-radius: 18px; display: flex; align-items: center; justify-content: center; font-size: 24px; flex-shrink: 0; transition: transform 0.3s ease; }
.kpi-card:hover .kpi-icon { transform: scale(1.1); }
.kpi-info { flex: 1; }
.kpi-value { font-size: 28px; font-weight: 800; color: var(--text); line-height: 1.1; margin-bottom: 4px; }
.kpi-label { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.8; }


/* Status KPI Variants */
.kpi-total .kpi-icon { background: #eff6ff; color: #2563eb; }
.kpi-submitted .kpi-icon { background: #f5f3ff; color: #7c3aed; }
.kpi-processing .kpi-icon { background: #ecfeff; color: #0891b2; }
.kpi-approved .kpi-icon { background: #f0fdf4; color: #16a34a; }
.kpi-revenue .kpi-icon { background: #fff7ed; color: #ea580c; }
.kpi-profit .kpi-icon { background: #fdf2f8; color: #db2777; }

/* Recent Apps Section */
.section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.section-title { font-size: 18px; font-weight: 700; color: var(--text); margin: 0; }

.app-table-card { background: #fff; border: 1px solid var(--border); border-radius: 24px; overflow: hidden; box-shadow: var(--shadow-sm); }
.app-table-header { padding: 24px 32px; border-bottom: 1px solid var(--border); background: #fcfcfd; }


@media(max-width:767px){
  .dashboard-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }
  .kpi-grid {
    grid-template-columns: 1fr; /* Stack KPI cards on small screens */
  }
  .section-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  .table-wrap { display: none; }
  .mobile-grid { display: grid; gap: 16px; }
}
@media(min-width:768px){
  .mobile-grid { display: none; }
}
</style>

<?php
// Enhanced KPI Queries
$stats = $pdo->query("SELECT 
    COUNT(*) as total,
    COUNT(CASE WHEN status='submitted' THEN 1 END) as submitted,
    COUNT(CASE WHEN status='processing' THEN 1 END) as processing,
    COUNT(CASE WHEN status='approved' THEN 1 END) as approved,
    SUM(CASE WHEN status IN ('processing','approved') THEN total_price ELSE 0 END) as revenue,
    SUM(CASE WHEN status IN ('processing','approved') THEN (total_price - cac_fee) ELSE 0 END) as profit
FROM applications")->fetch();

$recent = $pdo->query("SELECT a.id, a.status, a.total_price, a.created_at, u.full_name as user_name, s.service_name FROM applications a JOIN users u ON a.user_id=u.id JOIN services s ON a.service_id=s.id ORDER BY a.created_at DESC LIMIT 8")->fetchAll();
?>

<div class="dashboard-header">
    <div>
        <h1 class="dashboard-title">Admin Dashboard</h1>
        <p class="dashboard-subtitle">Monitor and manage your business performance in real-time.</p>
    </div>
    <div style="display:flex; gap:12px">
        <a href="<?=url('admin/applications.php')?>" class="btn primary"><?=ui_icon('applications',18)?> Manage Applications</a>
        <a href="<?=url('admin/settings.php')?>" class="btn btn-outline"><?=ui_icon('services',18)?> System Settings</a>
    </div>
</div>

<div class="kpi-grid">
    <div class="kpi-card kpi-total">
        <div class="kpi-icon"><?=ui_icon('applications', 24)?></div>
        <div class="kpi-info">
            <div class="kpi-value"><?=number_format((float)$stats['total'])?></div>
            <div class="kpi-label">Total Apps</div>
        </div>
    </div>
    <div class="kpi-card kpi-submitted">
        <div class="kpi-icon"><?=ui_icon('start', 24)?></div>
        <div class="kpi-info">
            <div class="kpi-value"><?=number_format((float)$stats['submitted'])?></div>
            <div class="kpi-label">Pending Review</div>
        </div>
    </div>
    <div class="kpi-card kpi-processing">
        <div class="kpi-icon"><?=ui_icon('processing', 24)?></div>
        <div class="kpi-info">
            <div class="kpi-value"><?=number_format((float)$stats['processing'])?></div>
            <div class="kpi-label">Processing</div>
        </div>
    </div>
    <div class="kpi-card kpi-approved">
        <div class="kpi-icon"><?=ui_icon('approved', 24)?></div>
        <div class="kpi-info">
            <div class="kpi-value"><?=number_format((float)$stats['approved'])?></div>
            <div class="kpi-label">Completed</div>
        </div>
    </div>
    <div class="kpi-card kpi-revenue">
        <div class="kpi-icon">₦</div>
        <div class="kpi-info">
            <div class="kpi-value">₦<?=e(money((float)$stats['revenue']))?></div>
            <div class="kpi-label">Revenue</div>
        </div>
    </div>
    <div class="kpi-card kpi-profit">
        <div class="kpi-icon"><?=ui_icon('check', 24)?></div>
        <div class="kpi-info">
            <div class="kpi-value">₦<?=e(money((float)$stats['profit']))?></div>
            <div class="kpi-label">Net Profit</div>
        </div>
    </div>
</div>

<div class="section-header">
    <h3 class="section-title">Recent Activity</h3>
    <a href="<?=url('admin/applications.php')?>" style="font-size:14px; font-weight:600; color:var(--primary); text-decoration:none">View All &rarr;</a>
</div>

<div class="app-table-card">
    <div class="table-wrap">
        <table class="striped">
            <thead>
                <tr>
                    <th style="padding-left:24px">Application ID</th>
                    <th>Customer</th>
                    <th>Service Type</th>
                    <th>Amount</th>
                    <th>Status</th>
                    <th>Date</th>
                    <th style="padding-right:24px; text-align:right">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($recent as $r): $st = $r['status']; ?>
                <tr>
                    <td style="padding-left:24px"><span style="font-weight:700; color:var(--muted)">#<?=e((string)$r['id'])?></span></td>
                    <td><div style="font-weight:600; color:var(--text)"><?=e($r['user_name'])?></div></td>
                    <td><div style="font-size:13px"><?=e($r['service_name'])?></div></td>
                    <td><div style="font-weight:700; color:var(--primary)">₦<?=e(money((float)$r['total_price']))?></div></td>
                    <td>
                        <span class="chip chip-<?=e($st)?>" style="text-transform:capitalize"><?=e($st)?></span>
                    </td>
                    <td><div style="font-size:12px; color:var(--muted)"><?=date('M j, Y', strtotime($r['created_at']))?></div></td>
                    <td style="padding-right:24px; text-align:right">
                        <a class="btn" href="<?=url('admin/applications.php?view='.e((string)$r['id']))?>" style="padding:6px 12px; font-size:13px">Details</a>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <div class="mobile-grid" style="padding:16px">
        <?php foreach ($recent as $r): $st = $r['status']; ?>
            <div class="card" style="margin-bottom:0; padding:16px; border-radius:12px">
                <div style="display:flex; justify-content:space-between; align-items:flex-start; margin-bottom:12px">
                    <span style="font-weight:700; color:var(--muted)">#<?=e((string)$r['id'])?></span>
                    <span class="chip chip-<?=e($st)?>"><?=e($st)?></span>
                </div>
                <div style="font-weight:700; margin-bottom:4px"><?=e($r['user_name'])?></div>
                <div style="font-size:13px; color:var(--muted); margin-bottom:12px"><?=e($r['service_name'])?></div>
                <div style="display:flex; justify-content:space-between; align-items:center; border-top:1px solid #f1f5f9; padding-top:12px">
                    <div style="font-weight:700; color:var(--primary)">₦<?=e(money((float)$r['total_price']))?></div>
                    <a class="btn" href="<?=url('admin/applications.php?view='.e((string)$r['id']))?>" style="padding:6px 12px; font-size:12px">View</a>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
