<?php
/**
 * Wishlist API - AJAX Endpoints
 * Digital Marketplace
 */

require_once __DIR__ . '/../config/database.php';

header('Content-Type: application/json');

$action = $_POST['action'] ?? $_GET['action'] ?? '';

// Require login for wishlist operations
if (!is_user_logged_in() && in_array($action, ['add', 'remove', 'toggle'])) {
    json_response(['success' => false, 'message' => 'Please login first', 'login_required' => true]);
}

switch ($action) {
    case 'add':
        handle_add_to_wishlist();
        break;
    
    case 'remove':
        handle_remove_from_wishlist();
        break;
    
    case 'toggle':
        handle_toggle_wishlist();
        break;
    
    case 'list':
        handle_get_wishlist();
        break;
    
    case 'check':
        handle_check_wishlist();
        break;
    
    default:
        json_response(['success' => false, 'message' => 'Invalid action']);
}

function handle_add_to_wishlist() {
    global $pdo;
    
    if (!verify_csrf_token($_POST['csrf_token'] ?? '')) {
        json_response(['success' => false, 'message' => 'Invalid request']);
    }
    
    $product_id = (int)($_POST['product_id'] ?? 0);
    $user_id = get_current_user_id();
    
    if (!$product_id) {
        json_response(['success' => false, 'message' => 'Invalid product']);
    }
    
    // Check if product exists
    $product = get_product_by_id($product_id);
    if (!$product) {
        json_response(['success' => false, 'message' => 'Product not found']);
    }
    
    if (add_to_wishlist($product_id, $user_id)) {
        json_response([
            'success' => true,
            'message' => 'Added to wishlist',
            'in_wishlist' => true
        ]);
    } else {
        json_response(['success' => false, 'message' => 'Failed to add to wishlist']);
    }
}

function handle_remove_from_wishlist() {
    global $pdo;
    
    if (!verify_csrf_token($_POST['csrf_token'] ?? '')) {
        json_response(['success' => false, 'message' => 'Invalid request']);
    }
    
    $product_id = (int)($_POST['product_id'] ?? 0);
    $user_id = get_current_user_id();
    
    if (!$product_id) {
        json_response(['success' => false, 'message' => 'Invalid product']);
    }
    
    if (remove_from_wishlist($product_id, $user_id)) {
        json_response([
            'success' => true,
            'message' => 'Removed from wishlist',
            'in_wishlist' => false
        ]);
    } else {
        json_response(['success' => false, 'message' => 'Failed to remove from wishlist']);
    }
}

function handle_toggle_wishlist() {
    global $pdo;
    
    if (!verify_csrf_token($_POST['csrf_token'] ?? '')) {
        json_response(['success' => false, 'message' => 'Invalid request']);
    }
    
    $product_id = (int)($_POST['product_id'] ?? 0);
    $user_id = get_current_user_id();
    
    if (!$product_id) {
        json_response(['success' => false, 'message' => 'Invalid product']);
    }
    
    $is_in_wishlist = is_in_wishlist($product_id, $user_id);
    
    if ($is_in_wishlist) {
        if (remove_from_wishlist($product_id, $user_id)) {
            json_response([
                'success' => true,
                'message' => 'Removed from wishlist',
                'in_wishlist' => false
            ]);
        } else {
            json_response(['success' => false, 'message' => 'Failed to remove from wishlist']);
        }
    } else {
        if (add_to_wishlist($product_id, $user_id)) {
            json_response([
                'success' => true,
                'message' => 'Added to wishlist',
                'in_wishlist' => true
            ]);
        } else {
            json_response(['success' => false, 'message' => 'Failed to add to wishlist']);
        }
    }
}

function handle_get_wishlist() {
    global $pdo;
    
    if (!is_user_logged_in()) {
        json_response(['success' => false, 'message' => 'Please login first']);
    }
    
    $user_id = get_current_user_id();
    $items = get_wishlist_items($user_id);
    
    $formatted_items = [];
    foreach ($items as $item) {
        $formatted_items[] = [
            'id' => $item['id'],
            'product_id' => $item['product_id'],
            'name' => $item['name'],
            'slug' => $item['slug'],
            'image' => get_product_image_url($item),
            'price' => $item['sale_price'] ?: $item['price']
        ];
    }
    
    json_response([
        'success' => true,
        'items' => $formatted_items,
        'count' => count($formatted_items)
    ]);
}

function handle_check_wishlist() {
    global $pdo;
    
    if (!is_user_logged_in()) {
        json_response(['success' => true, 'in_wishlist' => false]);
    }
    
    $product_id = (int)($_GET['product_id'] ?? 0);
    $user_id = get_current_user_id();
    
    json_response([
        'success' => true,
        'in_wishlist' => is_in_wishlist($product_id, $user_id)
    ]);
}
