<?php
session_start();
require_once __DIR__ . '/../includes/session_check.php';
require_once __DIR__ . '/../includes/url_helper.php';

$currentPage = basename($_SERVER['PHP_SELF']);
$theme = $_COOKIE['theme'] ?? 'light';
?>
<!DOCTYPE html>
<html lang="es" data-bs-theme="<?= $theme ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $pageTitle ?? 'Sistema de Mensajería' ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote-lite.min.css" rel="stylesheet">
    <style>
        :root {
            --discord-color: #5865F2;
            --telegram-color: #0088cc;
        }
        [data-bs-theme="dark"] {
            --sidebar-bg: #1a1a2e;
        }
        [data-bs-theme="light"] {
            --sidebar-bg: #1a1a2e;
        }
        .sidebar {
            min-height: 100vh;
            background: var(--sidebar-bg);
        }
        [data-bs-theme="dark"] .card-header.bg-white,
        [data-bs-theme="dark"] .bg-white:not(.btn):not(.nav-link):not(.dropdown-menu) {
            background-color: var(--bs-body-bg) !important;
        }
        [data-bs-theme="dark"] .bg-light {
            background-color: var(--bs-tertiary-bg) !important;
        }
        .sidebar .nav-link {
            color: rgba(255,255,255,0.7);
            padding: 12px 20px;
            border-radius: 8px;
            margin: 4px 8px;
        }
        #theme-toggle {
            color: rgba(255,255,255,0.7);
            border: none;
            background: transparent;
        }
        #theme-toggle:hover {
            color: white;
        }
        .sidebar .nav-link:hover, .sidebar .nav-link.active {
            background: rgba(255,255,255,0.1);
            color: white;
        }
        .platform-discord { color: var(--discord-color); }
        .platform-telegram { color: var(--telegram-color); }
        .status-pending { color: #ffc107; }
        .status-sent { color: #28a745; }
        .status-failed { color: #dc3545; }
        .status-processing { color: #17a2b8; }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <nav class="col-md-2 d-none d-md-block sidebar py-3">
                <div class="text-center mb-4 position-relative">
                    <button class="btn btn-sm position-absolute end-0 me-2" id="theme-toggle" title="Cambiar tema">
                        <i class="bi bi-moon-fill"></i>
                    </button>
                    <h5 class="text-white"><i class="bi bi-messenger"></i> Mensajería</h5>
                    <small class="text-muted">Discord & Telegram</small>
                </div>
                
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'index.php' ? 'active' : '' ?>" href="/index.php">
                            <i class="bi bi-house-door"></i> Inicio
                        </a>
                    </li>

                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'create_message.php' ? 'active' : '' ?>" href="/create_message.php">
                            <i class="bi bi-plus-circle"></i> Crear Mensaje
                        </a>
                    </li>

                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'scheduled_messages.php' ? 'active' : '' ?>" href="/scheduled_messages.php">
                            <i class="bi bi-clock"></i> Programados
                        </a>
                    </li>

                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'recurrentes.php' ? 'active' : '' ?>" href="/recurrentes.php">
                            <i class="bi bi-repeat"></i> Plantillas
                        </a>
                    </li>

                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'sent_messages.php' ? 'active' : '' ?>" href="/sent_messages.php">
                            <i class="bi bi-send"></i> Enviados
                        </a>
                    </li>

                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'gallery.php' ? 'active' : '' ?>" href="/gallery.php">
                            <i class="bi bi-images"></i> Galería
                        </a>
                    </li>
                    
                    <?php if (isAdmin()): ?>
                    <li class="nav-item mt-3">
                        <small class="text-muted px-3">ADMINISTRACIÓN</small>
                    </li>
                    
                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'users.php' ? 'active' : '' ?>" href="/admin/users.php">
                            <i class="bi bi-people"></i> Usuarios
                        </a>
                    </li>
                    
                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'recipients.php' ? 'active' : '' ?>" href="/admin/recipients.php">
                            <i class="bi bi-person-check"></i> Destinatarios
                        </a>
                    </li>
                    
                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'languages.php' ? 'active' : '' ?>" href="/admin/languages.php">
                            <i class="bi bi-translate"></i> Idiomas
                        </a>
                    </li>
                    
                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'comandos.php' ? 'active' : '' ?>" href="/admin/comandos.php">
                            <i class="bi bi-terminal"></i> Comandos
                        </a>
                    </li>
                    
                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'test_discord_connection.php' ? 'active' : '' ?>" href="/admin/test_discord_connection.php">
                            <i class="bi bi-discord"></i> Test Discord
                        </a>
                    </li>

                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'ia_agent.php' ? 'active' : '' ?>" href="/admin/ia_agent.php">
                            <i class="bi bi-robot"></i> Config IA
                        </a>
                    </li>
                    
                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'set_webhook.php' ? 'active' : '' ?>" href="/set_webhook.php">
                            <i class="bi bi-telegram"></i> Webhook Telegram
                        </a>
                    </li>
                    
                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'chat_telegram.php' ? 'active' : '' ?>" href="/chat_telegram.php">
                            <i class="bi bi-chat-dots"></i> Chat Telegram
                        </a>
                    </li>
                    
                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'telegram_bot_interactions.php' ? 'active' : '' ?>" href="/telegram/admin/telegram_bot_interactions.php">
                            <i class="bi bi-robot"></i> Bot Telegram
                        </a>
                    </li>
                    
                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'telegram_welcome.php' ? 'active' : '' ?>" href="/telegram/admin/telegram_welcome.php">
                            <i class="bi bi-person-plus"></i> Bienvenida Grupos
                        </a>
                    </li>
                    
                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'translate_message.php' ? 'active' : '' ?>" href="/translate_message.php">
                            <i class="bi bi-translate"></i> Traductor
                        </a>
                    </li>
                    
                    <li class="nav-item">
                        <a class="nav-link <?= $currentPage === 'admin_send_message.php' ? 'active' : '' ?>" href="/admin_send_message.php">
                            <i class="bi bi-send"></i> Enviar Directo
                        </a>
                    </li>
                    <?php endif; ?>
                </ul>
                
                <div class="mt-auto pt-3 border-top">
                    <a class="nav-link <?= $currentPage === 'profile.php' ? 'active' : '' ?>" href="/profile.php">
                        <i class="bi bi-person"></i> Perfil
                    </a>
                    <a class="nav-link" href="/logout.php">
                        <i class="bi bi-box-arrow-left"></i> Cerrar Sesión
                    </a>
                </div>
            </nav>
            
            <main class="col-md-10 ms-sm-auto px-4 py-4">
