<?php
namespace App\Controllers;

require_once BASE_PATH . '/app/helpers/permissions.php';

use App\Core\Controller;
use App\Core\Database;
use App\Middleware\RoleGuard;

class SuperAdminDashboardController extends Controller
{
    public function index(): void
    {
        RoleGuard::allow(['superadmin']);

        $db = Database::getInstance();

        $stats = [
            'organizations' => (int)$db->query("SELECT COUNT(*) FROM organizations")->fetchColumn(),
            'conferences'   => (int)$db->query("SELECT COUNT(*) FROM conferences")->fetchColumn(),
            'submissions'   => (int)$db->query("SELECT COUNT(*) FROM submissions")->fetchColumn(),
            'reviews'       => (int)$db->query("SELECT COUNT(*) FROM reviews")->fetchColumn(),
            'users'         => (int)$db->query("SELECT COUNT(*) FROM users")->fetchColumn(),
            'reviewers'     => (int)$db->query("SELECT COUNT(*) FROM users WHERE role_id=3")->fetchColumn(),
        ];

        $latestSubmissions = $db->query("
            SELECT id,
                   COALESCE(title_en, title_ar) AS title,
                   status,
                   created_at
            FROM submissions
            ORDER BY id DESC
            LIMIT 5
        ")->fetchAll();

        $statusDistribution = $db->query("
            SELECT status, COUNT(*) AS total
            FROM submissions
            GROUP BY status
        ")->fetchAll();

        ob_start();
        require BASE_PATH.'/app/views/admin/dashboard/index.php';
        $content = ob_get_clean();

        require BASE_PATH.'/app/views/layouts/admin_layout.php';
    }
}
