<?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 PublicProceedingsController extends Controller
{
    public function index(): void
    {
        $orgId = (int) current_org_id();
        $db=Database::getInstance();
        $cid=(int)($_GET['conference_id']??0);
        if($cid<=0){ http_response_code(404); echo "Missing conference_id"; return; }

        $st=$db->prepare("
          SELECT s.id,s.title_en,s.title_ar,
                 p.doi,p.published_at
          FROM proceedings p
          JOIN submissions s ON s.id=p.submission_id
          WHERE p.conference_id=:c AND p.is_published=1
          AND conference_id IN (SELECT id FROM conferences WHERE org_id=".(int)$orgId.")  ORDER BY p.published_at DESC, s.id DESC
        ");
        $st->execute([':c'=>$cid, ':org_id' => $orgId]);
        $rows=$st->fetchAll()?:[];

        $this->view('public/proceedings',['rows'=>$rows,'conference_id'=>$cid]);
    }
}
