<?php
declare(strict_types=1);

namespace App\Controllers;

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

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

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

final class AdminSubmissionsStableController extends Controller
{
    public function index(): void
    {
        require_admin_area();
        require_org_context();

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

        $q = trim((string)($_GET['q'] ?? ''));

        $sql = "
          SELECT s.*, c.title_en, c.title_ar
          FROM submissions s
          JOIN conferences c ON c.id=s.conference_id
          WHERE c.org_id=:org_id
        ";
        $params = [':org_id'=>$orgId];

        if ($q !== '') {
          $sql .= " AND (s.id=:qid OR s.title_en LIKE :qs OR s.title_ar LIKE :qs)";
          $params[':qid'] = (int)$q;
          $params[':qs']  = "%$q%";
        }

        $sql .= " ORDER BY s.id DESC LIMIT 300";

        $st = $db->prepare($sql);
        $st->execute($params);
        $rows = $st->fetchAll() ?: [];

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

$stmtReviewers = $db->query("
    SELECT
        id,
        first_name,
        last_name,
        email
    FROM users
    WHERE role_id = 4
    AND is_active = 1
    ORDER BY first_name ASC, last_name ASC
");

$reviewers =
    $stmtReviewers->fetchAll(PDO::FETCH_ASSOC);

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