<?php
// Wishlist management
if (session_status() !== PHP_SESSION_ACTIVE) session_start();

function addToWishlist($userId, $productId) {
    $wishlistFile = __DIR__ . '/data/wishlist.json';
    $wishlist = [];
    
    if (file_exists($wishlistFile)) {
        $wishlist = json_decode(file_get_contents($wishlistFile), true) ?: [];
    }
    
    if (!isset($wishlist[$userId])) {
        $wishlist[$userId] = [];
    }
    
    if (!in_array($productId, $wishlist[$userId])) {
        $wishlist[$userId][] = $productId;
        file_put_contents($wishlistFile, json_encode($wishlist, JSON_PRETTY_PRINT));
        return true;
    }
    return false;
}

function removeFromWishlist($userId, $productId) {
    $wishlistFile = __DIR__ . '/data/wishlist.json';
    $wishlist = [];
    
    if (file_exists($wishlistFile)) {
        $wishlist = json_decode(file_get_contents($wishlistFile), true) ?: [];
    }
    
    if (isset($wishlist[$userId])) {
        $wishlist[$userId] = array_values(array_filter($wishlist[$userId], function($id) use ($productId) {
            return $id != $productId;
        }));
        file_put_contents($wishlistFile, json_encode($wishlist, JSON_PRETTY_PRINT));
        return true;
    }
    return false;
}

function isInWishlist($userId, $productId) {
    $wishlistFile = __DIR__ . '/data/wishlist.json';
    
    if (!file_exists($wishlistFile)) return false;
    
    $wishlist = json_decode(file_get_contents($wishlistFile), true) ?: [];
    
    return isset($wishlist[$userId]) && in_array($productId, $wishlist[$userId]);
}

function getWishlistItems($userId) {
    $wishlistFile = __DIR__ . '/data/wishlist.json';
    
    if (!file_exists($wishlistFile)) return [];
    
    $wishlist = json_decode(file_get_contents($wishlistFile), true) ?: [];
    
    $items = $wishlist[$userId] ?? [];
    
    // Ensure we return an array and filter out any invalid entries
    if (!is_array($items)) {
        return [];
    }
    
    // Filter out any non-numeric or invalid IDs
    return array_values(array_filter($items, function($id) {
        return is_numeric($id) && $id > 0;
    }));
}

// Handle AJAX wishlist requests
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    $userId = $_SESSION['user_id'] ?? 'guest_' . md5($_SERVER['REMOTE_ADDR']);
    
    if ($_POST['action'] === 'wishlist') {
        $productId = (int)($_POST['product_id'] ?? 0);
        
        if ($productId > 0) {
            $isInWishlist = isInWishlist($userId, $productId);
            
            if ($isInWishlist) {
                removeFromWishlist($userId, $productId);
                echo json_encode(['success' => true, 'action' => 'removed']);
            } else {
                addToWishlist($userId, $productId);
                echo json_encode(['success' => true, 'action' => 'added']);
            }
            exit;
        }
    }
    
    if ($_POST['action'] === 'check_wishlist') {
        $productId = (int)($_POST['product_id'] ?? 0);
        
        if ($productId > 0) {
            $isInWishlist = isInWishlist($userId, $productId);
            echo json_encode(['success' => true, 'in_wishlist' => $isInWishlist]);
            exit;
        }
    }
}
?>
