<?php
// Pack511: enforce automatic conference scope for non-superadmin layouts.
if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/helpers/conference_scope.php')) {
    require_once BASE_PATH . '/app/helpers/conference_scope.php';
}

if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/views/partials/i18n_sidebar_labels.php')) {
    require_once BASE_PATH . '/app/views/partials/i18n_sidebar_labels.php';
}

if (function_exists('ec_scope_role') && ec_scope_role() !== 'super_admin' && function_exists('ec_scope_auto_select_conference')) {
    ec_scope_auto_select_conference(false);
}
?>

<?php if (!empty($_SESSION['success'])): ?>

<div class="alert alert-success">

<?= htmlspecialchars($_SESSION['success']) ?>

</div>

<?php unset($_SESSION['success']); ?>

<?php endif; ?>

<?php
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$content = $content ?? '';
$userName = $_SESSION['user_name'] ?? ($_SESSION['email'] ?? 'Reviewer');
$userEmail = $_SESSION['email'] ?? '';
$conferenceId = $_SESSION['conference_id'] ?? $_SESSION['current_conference_id'] ?? null;
$roleCode = $_SESSION['conference_role'] ?? $_SESSION['role'] ?? 'reviewer';

if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/helpers/i18n.php')) {
    require_once BASE_PATH . '/app/helpers/i18n.php';
}
if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/helpers/easychair_translate.php')) {
    require_once BASE_PATH . '/app/helpers/easychair_translate.php';
}

if (!function_exists('reviewer_layout_h')) {
    function reviewer_layout_h($v) {
        return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
    }
}

if (!function_exists('reviewer_layout_t')) {
    function reviewer_layout_t(string $key, string $fallback): string {
        if (function_exists('ec_t')) return ec_t($key, $fallback);
        if (function_exists('t')) return t($key, $fallback);
        return $fallback;
    }
}

$lang = function_exists('ec_lang') ? ec_lang() : 'ar';
$dir = function_exists('ec_dir') ? ec_dir() : ($lang === 'en' ? 'ltr' : 'rtl');
$isRtl = $dir === 'rtl';

$conferenceShort = $_SESSION['conference_short_name'] ?? ($conferenceId ? ('Conference #' . $conferenceId) : '');
$conferenceTitle = '';

try {
    if (defined('BASE_PATH') && class_exists('\App\Core\Database') && $conferenceId) {
        $db = \App\Core\Database::getInstance();
        $stmt = $db->prepare("SELECT short_name, title_en, title_ar FROM conferences WHERE id=? LIMIT 1");
        $stmt->execute([(int)$conferenceId]);
        $c = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($c) {
            $conferenceShort = $c['short_name'] ?: $conferenceShort;
            $conferenceTitle = $lang === 'en'
                ? ($c['title_en'] ?: $c['title_ar'] ?: '')
                : ($c['title_ar'] ?: $c['title_en'] ?: '');
        }
    }
} catch (Throwable $e) {}


$links = [

    [
        'url' => '/easychair/reviewer/dashboard',
        'icon' => 'fa-gauge-high',
        'label' => reviewer_layout_t(
            'menu.reviewer_dashboard',
            'Reviewer Dashboard'
        ),
    ],

    [
        'url' => '/easychair/reviewer/assignments',
        'icon' => 'fa-clipboard-check',
        'label' => reviewer_layout_t(
            'menu.reviewer_assignments',
            'Review Assignments'
        ),
    ],

    [
        'url' => '/easychair/reviewer/history',
        'icon' => 'fa-clock-rotate-left',
        'label' => reviewer_layout_t(
            'menu.reviewer_history',
            'Review History'
        ),
    ],

    [
        'url' => '/easychair/profile',
        'icon' => 'fa-user-graduate',
        'label' => reviewer_layout_t(
            'layout.academic_profile',
            'Academic Profile'
        ),
    ],

    [
        'url' => '/easychair/notifications',
        'icon' => 'fa-bell',
        'label' => reviewer_layout_t(
            'layout.notifications',
            'Notifications'
        ),
    ],

    [
        'url' => '/easychair/logout',
        'icon' => 'fa-right-from-bracket',
        'label' => reviewer_layout_t(
            'menu.logout',
            'Logout'
        ),
    ],

];

/*
Conditional conference switch
*/

$conferenceList =
    $_SESSION['conference_list'] ?? [];

if (is_array($conferenceList)
    && count($conferenceList) > 1) {

    $links[] = [
        'url' => '/easychair/select-conference',
        'icon' => 'fa-building-columns',
        'label' => reviewer_layout_t(
            'menu.change_conference',
            'Change Conference'
        ),
    ];

}


$currentPath = parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH) ?: '';
?>
<!DOCTYPE html>
<html lang="<?php echo reviewer_layout_h($lang); ?>" dir="<?php echo reviewer_layout_h($dir); ?>">
<head>
    <meta charset="UTF-8">
    <title><?php echo reviewer_layout_h(reviewer_layout_t('menu.reviewer_dashboard', 'Reviewer Dashboard')); ?> - EasyChair</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;800;900&display=swap" rel="stylesheet">
    <?php if ($isRtl): ?>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
    <?php else: ?>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <?php endif; ?>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css" rel="stylesheet">

    <style>
        :root {
            --ec-sidebar-w: 270px;
            --ec-blue: #1d4ed8;
            --ec-navy: #0f172a;
            --ec-bg: #f4f7fb;
        }

        * { box-sizing: border-box; }

        html, body {
            min-height: 100%;
            max-width: 100%;
            overflow-x: hidden;
        }

        body {
            margin: 0;
            font-family: 'Cairo', sans-serif;
            background: var(--ec-bg);
            color: #111827;
            direction: <?php echo $isRtl ? 'rtl' : 'ltr'; ?>;
            text-align: <?php echo $isRtl ? 'right' : 'left'; ?>;
        }

        .reviewer-sidebar {
            position: fixed;
            top: 0;
            bottom: 0;
            <?php echo $isRtl ? 'right' : 'left'; ?>: 0;
            width: var(--ec-sidebar-w);
            z-index: 1040;
            overflow-y: auto;
            padding: 20px 14px;
            color: #fff;
            background: linear-gradient(180deg, #111827, #1d4ed8);
            box-shadow: 0 20px 45px rgba(15,23,42,.22);
        }

        .reviewer-brand {
            display: flex;
            align-items: center;
            gap: 10px;
            font-weight: 900;
            font-size: 20px;
            margin-bottom: 18px;
            padding: 8px 8px 14px;
        }

        .reviewer-brand i { font-size: 23px; }

        .reviewer-lang-switch {
            display: flex;
            gap: 8px;
            margin: 0 6px 16px;
        }

        .reviewer-lang-switch a {
            flex: 1;
            text-align: center;
            text-decoration: none;
            border-radius: 999px;
            padding: 7px 10px;
            font-weight: 900;
            color: #1d4ed8;
            background: #fff;
        }

        .reviewer-lang-switch a.active {
            color: #fff;
            background: #2563eb;
            box-shadow: inset 0 0 0 1px rgba(255,255,255,.35);
        }

        .reviewer-conf,
        .reviewer-user {
            background: rgba(255,255,255,.08);
            border: 1px solid rgba(255,255,255,.12);
            border-radius: 18px;
            padding: 12px;
            margin: 0 6px 16px;
        }

        .reviewer-conf .short,
        .reviewer-user .name {
            font-weight: 900;
        }

        .reviewer-conf .title,
        .reviewer-user .meta {
            opacity: .78;
            font-size: 12px;
            line-height: 1.6;
            margin-top: 4px;
            overflow-wrap: anywhere;
        }

        .reviewer-link {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 11px 12px;
            margin-bottom: 7px;
            border-radius: 14px;
            color: rgba(255,255,255,.92);
            text-decoration: none;
            font-weight: 800;
            transition: .16s ease;
        }

        .reviewer-link i {
            width: 22px;
            text-align: center;
        }

        .reviewer-link:hover,
        .reviewer-link.active {
            color: #fff;
            background: rgba(255,255,255,.15);
            transform: translateY(-1px);
        }

        .reviewer-main {
            min-height: 100vh;
            width: calc(100% - var(--ec-sidebar-w));
            <?php echo $isRtl ? 'margin-right' : 'margin-left'; ?>: var(--ec-sidebar-w);
            <?php echo $isRtl ? 'margin-left' : 'margin-right'; ?>: 0;
        }

        .reviewer-topbar {
            min-height: 74px;
            background: #fff;
            border-bottom: 1px solid #e5e7eb;
            padding: 14px 24px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 14px;
            box-shadow: 0 8px 24px rgba(15,23,42,.04);
        }

        .reviewer-topbar-title {
            font-weight: 900;
            font-size: 16px;
        }

        .reviewer-topbar-subtitle {
            color: #64748b;
            font-size: 13px;
        }

        .reviewer-badge {
            background: #e0f2fe;
            color: #075985;
            border-radius: 999px;
            padding: 7px 12px;
            font-weight: 900;
            font-size: 12px;
            white-space: nowrap;
        }

        .reviewer-content {
            padding: 24px;
            max-width: 100%;
        }

        .container,
        .container-fluid {
            max-width: 100% !important;
        }

        .card,
        [class*="hero"],
        [class*="dashboard"] {
            max-width: 100%;
        }

        .table-responsive {
            width: 100%;
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
        }

        th, td {
            vertical-align: middle;
        }

        td {
            overflow-wrap: anywhere;
        }

        .mobile-menu-btn {
            display: none;
            border: 0;
            border-radius: 999px;
            background: #1d4ed8;
            color: #fff;
            font-weight: 900;
            padding: 9px 13px;
            box-shadow: 0 12px 30px rgba(15,23,42,.20);
        }

        .reviewer-overlay {
            display: none;
            position: fixed;
            inset: 0;
            z-index: 1030;
            background: rgba(15,23,42,.48);
        }

        body.reviewer-sidebar-open .reviewer-overlay {
            display: block;
        }

        @media (max-width: 1100px) {
            :root { --ec-sidebar-w: 250px; }
            .reviewer-content { padding: 18px; }
        }

        @media (max-width: 900px) {
            .mobile-menu-btn {
                display: inline-flex;
                align-items: center;
                gap: 8px;
            }

            .reviewer-sidebar {
                width: 285px;
                max-width: 86vw;
                transition: transform .22s ease;
                <?php if ($isRtl): ?>
                    transform: translateX(105%);
                <?php else: ?>
                    transform: translateX(-105%);
                <?php endif; ?>
            }

            body.reviewer-sidebar-open .reviewer-sidebar {
                transform: translateX(0);
            }

            .reviewer-main {
                width: 100%;
                margin-left: 0;
                margin-right: 0;
            }

            .reviewer-topbar {
                padding: 12px 14px;
                position: sticky;
                top: 0;
                z-index: 1020;
            }

            .reviewer-content {
                padding: 14px;
            }
        }

        @media (max-width: 576px) {
            .reviewer-topbar {
                align-items: flex-start;
                flex-direction: column;
            }

            .reviewer-badge {
                white-space: normal;
            }

            h1, .h1 { font-size: 1.55rem !important; }
            h2, .h2 { font-size: 1.35rem !important; }
            h3, .h3 { font-size: 1.18rem !important; }
            table { font-size: 13px; }
        }
    </style>
</head>

<body class="<?php echo $isRtl ? 'rtl' : 'ltr'; ?>">
    <aside class="reviewer-sidebar">
        <div class="reviewer-brand">
            <i class="fa-solid fa-clipboard-check"></i>
            <span>EasyChair Reviewer</span>
        </div>

        <div class="reviewer-lang-switch">
            <a class="<?php echo $lang === 'ar' ? 'active' : ''; ?>" href="/easychair/lang?set=ar">عربي</a>
            <a class="<?php echo $lang === 'en' ? 'active' : ''; ?>" href="/easychair/lang?set=en">EN</a>
        </div>

        <div class="reviewer-conf">
            <div class="short"><?php echo reviewer_layout_h($conferenceShort); ?></div>
            <?php if ($conferenceTitle): ?>
                <div class="title"><?php echo reviewer_layout_h($conferenceTitle); ?></div>
            <?php endif; ?>
        </div>

        <div class="reviewer-user">
            <div class="name"><?php echo reviewer_layout_h($userName); ?></div>
            <div class="meta"><?php echo reviewer_layout_h($roleCode); ?><?php echo $userEmail ? ' | ' . reviewer_layout_h($userEmail) : ''; ?></div>
        </div>

        <nav>
            <?php foreach ($links as $link): ?>
                <?php
                    $url = $link['url'];
                    $active = rtrim($currentPath, '/') === rtrim($url, '/');
                ?>
                <a class="reviewer-link <?php echo $active ? 'active' : ''; ?>" href="<?php echo reviewer_layout_h($url); ?>">
                    <i class="fa-solid <?php echo reviewer_layout_h($link['icon']); ?>"></i>
                    <span><?php echo reviewer_layout_h($link['label']); ?></span>
                </a>
            <?php endforeach; ?>
        </nav>
    </aside>

    <div class="reviewer-overlay" onclick="document.body.classList.remove('reviewer-sidebar-open')"></div>

    <main class="reviewer-main">
        <div class="reviewer-topbar">
            <button type="button" class="mobile-menu-btn" onclick="document.body.classList.toggle('reviewer-sidebar-open')">
                <i class="fa-solid fa-bars"></i>
                <span><?php echo reviewer_layout_h(reviewer_layout_t('action.menu', 'Menu')); ?></span>
            </button>

            <div>
                <div class="reviewer-topbar-title"><?php echo reviewer_layout_h($userName); ?></div>
                <div class="reviewer-topbar-subtitle"><?php echo reviewer_layout_h(reviewer_layout_t('reviewer.dashboard.welcome', 'Reviewer Workspace')); ?></div>
            </div>

            <div class="reviewer-badge">
                <?php echo reviewer_layout_h($conferenceShort); ?>
            </div>
        </div>

        <div class="reviewer-content">
            <?php echo $content; ?>
        </div>
    </main>

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            document.querySelectorAll('table').forEach(function (table) {
                if (!table.parentElement.classList.contains('table-responsive')) {
                    const wrapper = document.createElement('div');
                    wrapper.className = 'table-responsive';
                    table.parentNode.insertBefore(wrapper, table);
                    wrapper.appendChild(table);
                }
            });

            document.querySelectorAll('.reviewer-sidebar a').forEach(function (a) {
                a.addEventListener('click', function () {
                    document.body.classList.remove('reviewer-sidebar-open');
                });
            });
        });
    </script>
<?php
if (defined('BASE_PATH') && file_exists(BASE_PATH . '/app/views/partials/toast.php')) {
    require BASE_PATH . '/app/views/partials/toast.php';
}
?>
</body>
</html>
