<?php
declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Database;

final class AdminRbacTenantController extends Controller
{
    public function index(): void
    {
        require_org_admin(); // org_admin أو superadmin
        require_org_context();

        $db = Database::getInstance();
        $orgId = (int) current_org_id();

        $st = $db->prepare("
          SELECT ou.id, ou.user_id, ou.role_code, ou.is_active, ou.created_at,
                 u.email, u.first_name, u.last_name
          FROM org_users ou
          JOIN users u ON u.id=ou.user_id
          WHERE ou.org_id=:o
          ORDER BY ou.role_code ASC, u.id DESC
          LIMIT 500
        ");
        $st->execute([':o'=>$orgId]);
        $rows = $st->fetchAll() ?: [];

        $this->view('admin/rbac/index', ['rows'=>$rows]);
    }
}
