<?php
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\Models\Conference;

class ConferenceController extends Controller
{
    public function index()
    {

        
        // Pack512: non-superadmin bypasses selector safely.
        if (function_exists('ec_scope_role') && ec_scope_role() !== 'superadmin') {
            if (function_exists('ec_scope_auto_select_conference')) {
                // ec_scope_auto_select_conference(true);
            }
            $landing = function_exists('ec_scope_landing_url') ? ec_scope_landing_url() : '/easychair/admin/dashboard';
            header("Location: " . $landing);
            exit;
        }

// Pack511B: non-superadmin selector bypass with role-safe landing.
        if (function_exists('ec_scope_role') && ec_scope_role() !== 'superadmin') {
            if (function_exists('ec_scope_auto_select_conference')) {
                // ec_scope_auto_select_conference(true);
            }
            $landing = function_exists('ec_scope_landing_url') ? ec_scope_landing_url() : '/easychair/admin/dashboard';
            header("Location: " . $landing);
            exit;
        }

$model = new Conference();
        $conferences = $model->all();
        $this->view('conference/index', compact('conferences'));
    }

    public function show()
    {

        
        // Pack512: non-superadmin bypasses selector safely.
        if (function_exists('ec_scope_role') && ec_scope_role() !== 'superadmin') {
            if (function_exists('ec_scope_auto_select_conference')) {
                // ec_scope_auto_select_conference(true);
            }
            $landing = function_exists('ec_scope_landing_url') ? ec_scope_landing_url() : '/easychair/admin/dashboard';
            header("Location: " . $landing);
            exit;
        }

// Pack511B: non-superadmin selector bypass with role-safe landing.
        if (function_exists('ec_scope_role') && ec_scope_role() !== 'superadmin') {
            if (function_exists('ec_scope_auto_select_conference')) {
                // ec_scope_auto_select_conference(true);
            }
            $landing = function_exists('ec_scope_landing_url') ? ec_scope_landing_url() : '/easychair/admin/dashboard';
            header("Location: " . $landing);
            exit;
        }

$id = $_GET['id'] ?? null;

/* Pack 614: bind conference submission context */

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$_SESSION['submit_conference_id'] = (int)$id;
$_SESSION['conference_id'] = (int)$id;

error_log(
"CONFERENCE CONTEXT SET FROM SHOW: " . $id
);

        $model = new Conference();
        $conference = $model->find($id);

        if (!$conference) {
            http_response_code(403);
            echo "Conference not accessible";
            return;
        }

        $this->view('conference/show', compact('conference'));
    }
}
