<?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;
use PDO;

final class AdminProceedingsTenantController extends Controller
{
    private function requireTenant(): array {
        require_admin_or_chair_tenant();
        require_org_context();
        $db=Database::getInstance();
        $orgId=(int)current_org_id();
        return [$db,$orgId];
    }

    public function index(): void
    {
        [$db,$orgId]=$this->requireTenant();

        $st=$db->prepare("SELECT id,title_en,title_ar FROM conferences WHERE org_id=:o ORDER BY id DESC");
        $st->execute([':o'=>$orgId]);
        $confs=$st->fetchAll(PDO::FETCH_ASSOC)?:[];
        $confId=(int)($_GET['conference_id'] ?? ($confs[0]['id'] ?? 0));

        $rows=[];
        if($confId>0){
            $st=$db->prepare("
              SELECT s.*
              FROM submissions s
              JOIN conferences c ON c.id=s.conference_id
              WHERE c.org_id=:o AND s.conference_id=:c
              ORDER BY s.id DESC
              LIMIT 400
            ");
            $st->execute([':o'=>$orgId,':c'=>$confId]);
            $rows=$st->fetchAll(PDO::FETCH_ASSOC)?:[];
        }

        $this->view('admin/proceedings/index', compact('confs','confId','rows'));
    }
}
