<?php

include(__DIR__ . '/../config/db.php');

if(!isset($_SESSION)){
    session_start();
}

$current_page = basename($_SERVER['PHP_SELF']);

// Back button logic
$back_pages = [
    'settings_logs.php'         => ['/pages/settings.php',  'Settings'],
    'settings_telefonia.php'    => ['/pages/settings.php',  'Settings'],
    'settings_campaigns.php'    => ['/pages/settings.php',  'Settings'],
    'settings_users.php'        => ['/pages/settings.php',  'Settings'],
    'settings_permissions.php'  => ['/pages/settings.php',  'Settings'],
    'settings_offices.php'      => ['/pages/settings.php',  'Settings'],
    'settings_hierarchy.php'    => ['/pages/settings.php',  'Settings'],
    'settings_products.php'     => ['/pages/settings.php',  'Settings'],
    'settings_integrations.php' => ['/pages/settings.php',  'Settings'],
    'settings_knowledge.php'    => ['/pages/settings.php',  'Settings'],
    'settings_companies.php'    => ['/pages/settings.php',  'Settings'],
    'settings_email.php'        => ['/pages/settings.php',  'Settings'],
    'ranking.php'               => ['/pages/settings.php',  'Settings'],
    'comparativas.php'          => ['/pages/settings.php',  'Settings'],
    'import_leads.php'          => ['/pages/settings.php',  'Settings'],
    'marcador.php'              => ['/pages/dashboard.php', 'Dashboard'],
    'metrics.php'               => ['/pages/dashboard.php', 'Dashboard'],
    'view_lead.php'             => ['/pages/leads.php',     'Leads'],
    'contract_view.php'         => ['/pages/contracts.php', 'Contracts'],
    'contract_edit.php'         => ['/pages/contracts.php', 'Contracts'],
];
$show_back  = isset($back_pages[$current_page]);
$back_url   = $show_back ? $back_pages[$current_page][0] : '';
$back_label = $show_back ? $back_pages[$current_page][1] : '';

/*
|--------------------------------------------------------------------------
| SETTINGS
|--------------------------------------------------------------------------
*/

$settings = [];

$settings_query = $conn->query("
SELECT *
FROM settings
LIMIT 1
");

if(
    $settings_query &&
    $settings_query->num_rows > 0
){
    $settings = $settings_query->fetch_assoc();
}

/*
|--------------------------------------------------------------------------
| LOGO
|--------------------------------------------------------------------------
*/

$logo_path = '/uploads/branding/logo.png';

if(
    isset($settings['company_logo']) &&
    trim($settings['company_logo']) != ''
){

    if(
        strpos($settings['company_logo'], '/uploads/') === false
    ){

        $logo_path =
        '/uploads/branding/' .
        trim($settings['company_logo']);

    } else {

        $logo_path =
        trim($settings['company_logo']);

    }

}

/*
|--------------------------------------------------------------------------
| PERMISOS
|--------------------------------------------------------------------------
*/

function canAccess($permission){

    return (
        isset($_SESSION[$permission]) &&
        intval($_SESSION[$permission]) === 1
    );

}

?>

<style>

.sidebar{
    position:fixed;
    left:0;
    top:0;
    width:260px;
    height:100vh;
    background:
    linear-gradient(
        180deg,
        #02122c,
        #031633,
        #041b40
    );
    padding:30px 22px;
    overflow-y:auto;
}

.logo{
    margin-bottom:35px;
    display:flex;
    align-items:center;
    justify-content:center;
}

.logo img{
    width:100%;
    max-width:180px;
    height:auto;
    object-fit:contain;
    display:block;
}

.sidebar-menu{
    list-style:none;
    padding:0;
    margin:0;
}

.sidebar-menu li{
    margin-bottom:10px;
}

.sidebar-menu a{
    display:flex;
    align-items:center;
    height:52px;
    padding:0 18px;
    border-radius:16px;
    text-decoration:none;
    color:#e5e7eb;
    font-size:15px;
    font-weight:600;
    transition:.2s;
}

.sidebar-menu a:hover{
    background:rgba(255,255,255,.06);
}

.sidebar-menu a.active{
    background:
    linear-gradient(
        90deg,
        #5b4bff,
        #7b61ff
    );
    color:#ffffff;
    box-shadow:
    0 10px 25px rgba(91,75,255,.35);
}

.logout-link{
    display:flex;
    align-items:center;
    justify-content:center;
    margin-top:30px;
    height:52px;
    border-radius:16px;
    text-decoration:none;
    background:#ef4444;
    color:#ffffff;
    font-size:15px;
    font-weight:700;
}

.back-link{
    display:flex;
    align-items:center;
    gap:8px;
    height:42px;
    padding:0 16px;
    border-radius:12px;
    background:rgba(255,255,255,.08);
    color:#e5e7eb;
    text-decoration:none;
    font-size:14px;
    font-weight:700;
    margin-bottom:20px;
    transition:.2s;
}

.back-link:hover{
    background:rgba(255,255,255,.14);
}

</style>

<div class="sidebar">

    <?php if($show_back): ?>
    <a href="<?= $back_url ?>" class="back-link">
        &#8592; <?= $back_label ?>
    </a>
    <?php endif; ?>

    <div class="logo">

        <img
            src="<?= htmlspecialchars($logo_path) ?>"
            alt="Logo"
        >

    </div>

    <ul class="sidebar-menu">

        <?php if(canAccess('can_view_dashboard')): ?>

        <li>
            <a
                href="/pages/dashboard.php"
                class="<?= $current_page == 'dashboard.php' ? 'active' : '' ?>"
            >
                Dashboard
            </a>
        </li>

        <?php endif; ?>

        <?php if(canAccess('can_view_leads')): ?>

        <li>
            <a
                href="/pages/leads.php"
                class="<?= $current_page == 'leads.php' ? 'active' : '' ?>"
            >
                Leads
            </a>
        </li>

        <?php endif; ?>

        <?php if(canAccess('can_view_contracts')): ?>

        <li>
            <a
                href="/pages/contracts.php"
                class="<?= $current_page == 'contracts.php' ? 'active' : '' ?>"
            >
                Contracts
            </a>
        </li>

        <?php endif; ?>

        <?php if(canAccess('can_view_pipeline')): ?>

        <li>
            <a
                href="/pages/pipeline.php"
                class="<?= $current_page == 'pipeline.php' ? 'active' : '' ?>"
            >
                Pipeline
            </a>
        </li>

        <?php endif; ?>

        <?php if(canAccess('can_view_reports')): ?>

        <li>
            <a
                href="/pages/reports.php"
                class="<?= $current_page == 'reports.php' ? 'active' : '' ?>"
            >
                Reports
            </a>
        </li>

        <?php endif; ?>

        <?php if(
            canAccess('can_view_campaign_metrics')
            ||
            canAccess('can_view_team_metrics')
            ||
            canAccess('can_view_office_metrics')
        ): ?>

        <li>
            <a
                href="/pages/metrics.php"
                class="<?= $current_page == 'metrics.php' ? 'active' : '' ?>"
            >
                Metrics
            </a>
        </li>

        <?php endif; ?>

        <?php if(canAccess('can_view_ranking')): ?>

        <li>
            <a
                href="/pages/ranking.php"
                class="<?= $current_page == 'ranking.php' ? 'active' : '' ?>"
            >
                🏆 Ranking
            </a>
        </li>

        <?php endif; ?>

        <?php if(canAccess('can_view_comparativas')): ?>

        <li>
            <a
                href="/pages/comparativas.php"
                class="<?= $current_page == 'comparativas.php' ? 'active' : '' ?>"
            >
                📊 Comparativas
            </a>
        </li>

        <?php endif; ?>

        <!-- AI ASSISTANT -->

        <li>
            <a
                href="/pages/ai_assistant.php"
                class="<?= $current_page == 'ai_assistant.php' ? 'active' : '' ?>"
            >
                AI Assistant
            </a>
        </li>

        <!-- MARCADOR -->

        <li>
            <a
                href="/pages/marcador.php"
                class="<?= $current_page == 'marcador.php' ? 'active' : '' ?>"
            >
                📞 Marcador
            </a>
        </li>

        <?php if(
            canAccess('can_manage_users')
            ||
            canAccess('can_manage_campaigns')
            ||
            canAccess('can_manage_offices')
            ||
            canAccess('can_manage_custom_fields')
        ): ?>

        <li>
            <a
                href="/pages/settings.php"
                class="<?= $current_page == 'settings.php' ? 'active' : '' ?>"
            >
                Settings
            </a>
        </li>

        <?php endif; ?>

    </ul>

    <a
        href="/logout.php"
        class="logout-link"
    >
        Logout
    </a>

</div>