<?php
require_once __DIR__ . '/shared/bootstrap.php';

// El bootstrap.php ya maneja la autenticación y carga $userData
// $userData está disponible globalmente a través de JWTAuth::getUserData() si se necesita de nuevo

// Logout
if (isset($_GET['logout'])) {
    setcookie('auth_token', '', time() - 3600, '/');
    header('Location: /login.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="<?php echo $userData->idioma ?? 'es'; ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo __('main_panel_title'); ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .header {
            background: white;
            border-radius: 15px;
            padding: 20px 30px;
            margin-bottom: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
        }
        
        .header h1 {
            color: #667eea;
            font-size: 24px;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .user-badge {
            background: #f0f0f0;
            padding: 8px 15px;
            border-radius: 20px;
            font-size: 14px;
            color: #333;
        }
        
        .rol-badge {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 6px 12px;
            border-radius: 15px;
            font-size: 12px;
            font-weight: 600;
        }
        
        .btn-logout {
            background: #ff6b6b;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
            transition: transform 0.2s;
        }
        
        .btn-logout:hover {
            transform: translateY(-2px);
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .platforms {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
            gap: 30px;
        }
        
        .platform-card {
            background: white;
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            transition: transform 0.3s, box-shadow 0.3s;
            cursor: pointer;
            text-decoration: none;
            color: inherit;
            display: block;
        }
        
        .platform-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 40px rgba(0, 0, 0, 0.2);
        }
        
        .platform-icon {
            font-size: 60px;
            margin-bottom: 20px;
            text-align: center;
        }
        
        .platform-title {
            font-size: 28px;
            font-weight: 700;
            margin-bottom: 15px;
            text-align: center;
        }
        
        .discord-card {
            border-top: 5px solid #5865F2;
        }
        
        .discord-card .platform-title {
            color: #5865F2;
        }
        
        .telegram-card {
            border-top: 5px solid #0088cc;
        }
        
        .telegram-card .platform-title {
            color: #0088cc;
        }
        
        .platform-description {
            text-align: center;
            color: #666;
            font-size: 14px;
            margin-bottom: 20px;
        }
        
        .platform-stats {
            display: flex;
            justify-content: space-around;
            margin-top: 25px;
            padding-top: 25px;
            border-top: 2px solid #f0f0f0;
        }
        
        .stat {
            text-align: center;
        }
        
        .stat-value {
            font-size: 24px;
            font-weight: 700;
            color: #333;
        }
        
        .stat-label {
            font-size: 12px;
            color: #999;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>🤖 <?php echo __('bot_admin_system_title'); ?></h1>
            <?php if (hasPermission('manage_languages')): ?>
            <a href="/shared/languages/manager.php" style="text-decoration: none;">
                <button class="btn-logout" style="background: #28a745; margin-right: 10px;">
                    <i class="fas fa-language"></i> <?php echo __('languages'); ?>
                </button>
            </a>
            <?php endif; ?>
            <div class="user-badge">
                👤 <?php echo htmlspecialchars($userData->username); ?>
            </div>
            <div class="rol-badge">
                <?php echo htmlspecialchars($userData->rol); ?>
            </div>
            <a href="?logout=1">
                <button class="btn-logout"><?php echo __('logout'); ?></button>
            </a>
        </div>
    </div>
    
    <div class="container">
        <div class="platforms">
            <!-- Tarjeta Discord -->
            <?php if (hasPermission('view_dashboard', 'discord')): ?>
            <a href="/discord/dashboard_discord.php" class="platform-card discord-card">
                <div class="platform-icon">💬</div>
                <h2 class="platform-title"><?php echo __('discord'); ?></h2>
                <p class="platform-description">
                    <?php echo __('discord_description'); ?>
                </p>
                <div class="platform-stats">
                    <div class="stat">
                        <div class="stat-value" id="discord-users">-</div>
                        <div class="stat-label"><?php echo __('users'); ?></div>
                    </div>
                    <div class="stat">
                        <div class="stat-value" id="discord-messages">-</div>
                        <div class="stat-label"><?php echo __('messages'); ?></div>
                    </div>
                    <div class="stat">
                        <div class="stat-value" id="discord-templates">-</div>
                        <div class="stat-label"><?php echo __('templates'); ?></div>
                    </div>
                </div>
            </a>
            <?php endif; ?>
            
            <!-- Tarjeta Telegram -->
            <?php if (hasPermission('view_dashboard', 'telegram')): ?>
            <a href="/telegram/dashboard_telegram.php" class="platform-card telegram-card">
                <div class="platform-icon">✈️</div>
                <h2 class="platform-title"><?php echo __('telegram'); ?></h2>
                <p class="platform-description">
                    <?php echo __('telegram_description'); ?>
                </p>
                <div class="platform-stats">
                    <div class="stat">
                        <div class="stat-value" id="telegram-users">-</div>
                        <div class="stat-label"><?php echo __('users'); ?></div>
                    </div>
                    <div class="stat">
                        <div class="stat-value" id="telegram-messages">-</div>
                        <div class="stat-label"><?php echo __('messages'); ?></div>
                    </div>
                    <div class="stat">
                        <div class="stat-value" id="telegram-templates">-</div>
                        <div class="stat-label"><?php echo __('templates'); ?></div>
                    </div>
                </div>
            </a>
            <?php endif; ?>
        </div>
    </div>
    
    <script>
        // Cargar estadísticas
        async function loadStats() {
            try {
                // Solo cargar si hay al menos una tarjeta visible (Discord o Telegram)
                if (document.querySelector('.platform-card')) {
                    const response = await fetch('/shared/api/stats.php');
                    const data = await response.json();
                    
                    if (data.success) {
                        // Discord
                        const discordUsers = document.getElementById('discord-users');
                        if (discordUsers) discordUsers.textContent = data.discord.users || 0;
                        const discordMessages = document.getElementById('discord-messages');
                        if (discordMessages) discordMessages.textContent = data.discord.messages || 0;
                        const discordTemplates = document.getElementById('discord-templates');
                        if (discordTemplates) discordTemplates.textContent = data.discord.templates || 0;
                        
                        // Telegram
                        const telegramUsers = document.getElementById('telegram-users');
                        if (telegramUsers) telegramUsers.textContent = data.telegram.users || 0;
                        const telegramMessages = document.getElementById('telegram-messages');
                        if (telegramMessages) telegramMessages.textContent = data.telegram.messages || 0;
                        const telegramTemplates = document.getElementById('telegram-templates');
                        if (telegramTemplates) telegramTemplates.textContent = data.telegram.templates || 0;
                    }
                }
            } catch (error) {
                console.error('Error cargando estadísticas:', error);
            }
        }
        
        loadStats();
    </script>
</body>
</html>