<?php
declare(strict_types=1);

namespace App\Controllers;

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

final class AdminReviewersListStableController extends Controller
{
    public function index(): void
    {
        require_admin_area();
        $orgId = (int) current_org_id();

        $db = Database::getInstance();

        // resolve reviewer role id
        $roleId = 4;
        try {
            $hasRoles = (int)$db->query("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=DATABASE() AND table_name='roles'")->fetchColumn() > 0;
            if ($hasRoles) {
                $rid = $db->query("SELECT id FROM roles WHERE code='reviewer' LIMIT 1")->fetchColumn();
                if ($rid) $roleId = (int)$rid;
            }
        } catch (\Throwable $e) {}

        $st = $db->prepare("SELECT id, first_name, last_name, email FROM users WHERE role_id=:r AND is_active=1 ORDER BY first_name,last_name LIMIT 500");
        $st->execute([':r'=>$roleId, ':org_id' => $orgId]);
        $rows = $st->fetchAll() ?: [];

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