<?php
// admin/addons.php
require_once '../config/db.php';


// 1. SEGURIDAD: Solo Administradores
if (!isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
    header("Location: ../index.php");
    exit;
}

// 2. LÓGICA DE ACTIVACIÓN / DESACTIVACIÓN
if (isset($_GET['toggle']) && isset($_GET['status'])) {
    $id = (int)$_GET['toggle'];
    $new_status = ($_GET['status'] == 'active') ? 'inactive' : 'active';
    
    $update = $pdo->prepare("UPDATE addons SET status = ? WHERE id = ?");
    $update->execute([$new_status, $id]);
    
    // CORRECCIÓN CRÍTICA: 
    // Usamos solo el nombre del archivo para recargar la página actual.
    // Esto evita que se genere la ruta http://localhost/vvault/admin/admin/
    header("Location: addons.php");
    exit;
}

// 3. OBTENER TODOS LOS ADDONS
$stmt = $pdo->query("SELECT * FROM addons ORDER BY id ASC");
$addons = $stmt->fetchAll(PDO::FETCH_ASSOC);

include '../includes/header.php';
?>

<div style="max-width: 900px; margin: 40px auto; padding: 0 20px; font-family: 'Inter', sans-serif;">
    
    <div style="margin-bottom: 30px; border-bottom: 1px solid var(--border); padding-bottom: 20px; display: flex; justify-content: space-between; align-items: center;">
        <div>
            <h2 style="color: white; margin: 0; font-size: 26px;">Addon Manager</h2>
            <p style="color: var(--text-gray); margin: 5px 0 0 0; font-size: 14px;">Administra las extensiones de tu marketplace.</p>
        </div>
        <a href="admin/index.php" style="color: var(--text-gray); text-decoration: none; font-size: 13px;"><i class="fa-solid fa-arrow-left"></i> Volver al Panel</a>
    </div>

    <div style="display: flex; flex-direction: column; gap: 15px;">
        <?php foreach($addons as $addon): ?>
            <div style="background: var(--nav-bg); border: 1px solid var(--border); border-radius: 12px; padding: 20px; display: flex; justify-content: space-between; align-items: center;">
                
                <div style="display: flex; align-items: center; gap: 20px;">
                    <div style="width: 45px; height: 45px; background: rgba(255,255,255,0.03); border-radius: 10px; display: flex; align-items: center; justify-content: center; color: var(--primary); border: 1px solid var(--border);">
                        <i class="fa-solid fa-puzzle-piece"></i>
                    </div>
                    <div>
                        <h3 style="color: white; margin: 0 0 4px 0; font-size: 16px; text-transform: capitalize; font-weight: 600;">
                            <?php echo str_replace('_', ' ', $addon['name']); ?>
                        </h3>
                        <span style="color: var(--text-gray); font-size: 12px; font-family: monospace; opacity: 0.7;">
                            /addons/<?php echo $addon['folder_name']; ?>/
                        </span>
                    </div>
                </div>

                <div style="display: flex; align-items: center; gap: 15px;">
                    
                    <div style="display: flex; gap: 10px;">
                        <?php if ($addon['folder_name'] === 'game_system'): ?>
                            <a href="admin/edit_hero.php" 
                               style="color: #00ff88; text-decoration: none; font-size: 12px; border: 1px solid rgba(0, 255, 136, 0.3); padding: 8px 15px; border-radius: 8px; background: rgba(0, 255, 136, 0.05); display: flex; align-items: center; gap: 6px;">
                               <i class="fa-solid fa-wand-magic-sparkles"></i> Editar Hero
                            </a>
                        <?php endif; ?>

                        <?php if ($addon['folder_name'] === 'global_announcements'): ?>
                            <a href="admin/addon_settings.php?name=<?php echo $addon['folder_name']; ?>" 
                               style="color: white; text-decoration: none; font-size: 12px; border: 1px solid var(--border); padding: 8px 15px; border-radius: 8px; background: rgba(255,255,255,0.02); display: flex; align-items: center; gap: 6px;">
                               <i class="fa-solid fa-sliders"></i> Configurar
                            </a>
                        <?php endif; ?>
                    </div>

                    <div style="width: 1px; height: 30px; background: var(--border);"></div>

                    <div style="display: flex; align-items: center; gap: 15px; min-width: 160px; justify-content: flex-end;">
                        <span style="font-size: 10px; font-weight: 800; letter-spacing: 1px; color: <?php echo $addon['status'] == 'active' ? 'var(--primary)' : '#666'; ?>;">
                            <?php echo strtoupper($addon['status']); ?>
                        </span>

                        <a href="admin/addons.php?toggle=<?php echo $addon['id']; ?>&status=<?php echo $addon['status']; ?>" 
                           style="padding: 8px 16px; border-radius: 8px; text-decoration: none; font-weight: 700; font-size: 11px; text-transform: uppercase; transition: 0.3s;
                                  background: <?php echo $addon['status'] == 'active' ? 'rgba(255, 77, 77, 0.1)' : 'var(--primary)'; ?>; 
                                  color: <?php echo $addon['status'] == 'active' ? '#ff4d4d' : 'black'; ?>;
                                  border: 1px solid <?php echo $addon['status'] == 'active' ? '#ff4d4d' : 'var(--primary)'; ?>;">
                            <?php echo $addon['status'] == 'active' ? 'Desactivar' : 'Activar'; ?>
                        </a>
                    </div>
                </div>

            </div>
        <?php endforeach; ?>
    </div>
</div>

<?php include '../includes/footer.php'; ?>