<?php
/**
 * Tom's Java Jive - Wishlist API
 */

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

require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/auth.php';

if (!CustomerAuth::isLoggedIn()) {
    jsonResponse(['error' => 'Please log in to manage your wishlist'], 401);
}

$customer = CustomerAuth::getFullUser();
$input = json_decode(file_get_contents('php://input'), true);
$action = $input['action'] ?? $_GET['action'] ?? '';
$productId = $input['product_id'] ?? $_GET['product_id'] ?? '';

switch ($action) {
    case 'add':
        if (empty($productId)) {
            jsonResponse(['error' => 'Product ID required'], 400);
        }
        
        // Check if product exists
        $product = db()->fetch("SELECT product_id FROM products WHERE product_id = :id", ['id' => $productId]);
        if (!$product) {
            jsonResponse(['error' => 'Product not found'], 404);
        }
        
        // Check if already in wishlist
        $existing = db()->fetch(
            "SELECT id FROM wishlist WHERE customer_id = :cid AND product_id = :pid",
            ['cid' => $customer['customer_id'], 'pid' => $productId]
        );
        
        if ($existing) {
            jsonResponse(['success' => true, 'message' => 'Already in wishlist']);
        }
        
        db()->insert('wishlist', [
            'customer_id' => $customer['customer_id'],
            'product_id' => $productId
        ]);
        
        jsonResponse(['success' => true, 'message' => 'Added to wishlist']);
        break;
        
    case 'remove':
        if (empty($productId)) {
            jsonResponse(['error' => 'Product ID required'], 400);
        }
        
        db()->query(
            "DELETE FROM wishlist WHERE customer_id = :cid AND product_id = :pid",
            ['cid' => $customer['customer_id'], 'pid' => $productId]
        );
        
        jsonResponse(['success' => true, 'message' => 'Removed from wishlist']);
        break;
        
    case 'check':
        if (empty($productId)) {
            jsonResponse(['error' => 'Product ID required'], 400);
        }
        
        $exists = db()->fetch(
            "SELECT id FROM wishlist WHERE customer_id = :cid AND product_id = :pid",
            ['cid' => $customer['customer_id'], 'pid' => $productId]
        );
        
        jsonResponse(['in_wishlist' => (bool)$exists]);
        break;
        
    case 'list':
        $items = db()->fetchAll(
            "SELECT p.product_id, p.name, p.slug, p.price, p.sale_price, p.images, p.stock
             FROM wishlist w
             JOIN products p ON w.product_id = p.product_id
             WHERE w.customer_id = :id
             ORDER BY w.created_at DESC",
            ['id' => $customer['customer_id']]
        );
        
        foreach ($items as &$item) {
            $item['images'] = json_decode($item['images'] ?? '[]', true);
        }
        
        jsonResponse(['items' => $items]);
        break;
        
    default:
        jsonResponse(['error' => 'Invalid action'], 400);
}
