<?php
namespace App\Controllers;

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

use App\Core\Database;

class AdminRoleController
{
    private function requireAccess(): void
    {
        if (session_status() !== PHP_SESSION_ACTIVE) {
            session_start();
        }

        if (empty($_SESSION['user_id'])) {
            header("Location: /easychair/login");
            exit;
        }

        $role = $_SESSION['role'] ?? $_SESSION['user_role'] ?? '';

        if (!in_array($role, ['super_admin','org_admin'], true)) {
            http_response_code(403);
            exit("Access denied.");
        }
    }

    public function index()
    {
$this->requireAccess();

        $db = Database::getInstance();
        $role = $_SESSION['role'] ?? '';
        $orgId = (int)($_SESSION['org_id'] ?? 0);

        if ($role === 'super_admin') {
            $stmt = $db->query("
                SELECT 
                    r.id,
                    r.name,
                    r.code,
                    COUNT(DISTINCT u.id) AS global_users_count,
                    COUNT(DISTINCT uc.user_id) AS conference_users_count
                FROM roles r
                LEFT JOIN users u ON u.role_id = r.id
                LEFT JOIN user_conferences uc ON uc.role_id = r.id
                GROUP BY r.id, r.name, r.code
                ORDER BY r.id
            ");
            $roles = $stmt->fetchAll(\PDO::FETCH_ASSOC);
        } else {
            $stmt = $db->prepare("
                SELECT 
                    r.id,
                    r.name,
                    r.code,
                    COUNT(DISTINCT u.id) AS global_users_count,
                    COUNT(DISTINCT uc.user_id) AS conference_users_count
                FROM roles r
                LEFT JOIN users u ON u.role_id = r.id AND u.org_id = ?
                LEFT JOIN user_conferences uc ON uc.role_id = r.id
                LEFT JOIN conferences c ON c.id = uc.conference_id AND c.org_id = ?
                GROUP BY r.id, r.name, r.code
                ORDER BY r.id
            ");
            $stmt->execute([$orgId, $orgId]);
            $roles = $stmt->fetchAll(\PDO::FETCH_ASSOC);
        }

        $matrix = [
            'super_admin' => ['Full system access', 'All organizations', 'All conferences', 'All data'],
            'org_admin' => ['Organization administration', 'Users in org', 'One current conference policy', 'Templates and homepage'],
            'admin' => ['Conference operations', 'Submissions', 'Reviewers', 'Workflow'],
            'organizer' => ['Operational management', 'Submissions', 'Reviewers', 'Homepage content'],
            'conference_chair' => ['Scientific workflow', 'Assignments', 'Decisions', 'Review form'],
            'track_chair' => ['Track review workflow', 'Assignments', 'Decisions'],
            'reviewer' => ['Assigned reviews only', 'Review form', 'Upload review files'],
            'author' => ['Own submissions', 'Revisions', 'Submission files'],
        ];

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

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