<?php
class SADashboardController {
    public function index(): void {
        SuperAuth::requireLogin();

        // Platform KPIs
        $tenantsTotal = db()->query("SELECT COUNT(*) FROM tenants")->fetchColumn();
        $tenantsActive = db()->query("SELECT COUNT(*) FROM tenants WHERE status='active'")->fetchColumn();
        $tenantsTrial = db()->query("SELECT COUNT(*) FROM tenants WHERE status='trial'")->fetchColumn();

        $activeSubs = db()->query("SELECT COUNT(*) FROM subscriptions WHERE status='active'")->fetchColumn();
        $totalRevenue = (float)db()->query("SELECT SUM(amount) FROM subscriptions WHERE status='active'")->fetchColumn();

        // Recent Tenants
        $recentTenants = db()->query("SELECT * FROM tenants ORDER BY created_at DESC LIMIT 5")->fetchAll();

        // Plan distribution
        $planStats = db()->query(
            "SELECT p.name, COUNT(s.id) as count FROM plans p 
             LEFT JOIN subscriptions s ON s.plan_id=p.id AND s.status='active' 
             GROUP BY p.id"
        )->fetchAll();

        $pageTitle = 'Platform Overview';
        ob_start();
        include APP_ROOT . '/views/superadmin/dashboard.php';
        $content = ob_get_clean();
        include APP_ROOT . '/views/superadmin/layout.php';
    }
}
