<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Wishlist;
use Illuminate\Http\Request;

class WishlistController extends Controller
{
    public function index(Request $request)
    {
        $items = Wishlist::with('product.category')->where('user_id',$request->user()->id)->get();
        return response()->json(['success'=>true,'data'=>$items]);
    }

    public function toggle(Request $request, int $productId)
    {
        $existing = Wishlist::where(['user_id'=>$request->user()->id,'product_id'=>$productId])->first();
        if($existing){ $existing->delete(); return response()->json(['success'=>true,'added'=>false,'message'=>'Removed from wishlist.']); }
        Wishlist::create(['user_id'=>$request->user()->id,'product_id'=>$productId]);
        return response()->json(['success'=>true,'added'=>true,'message'=>'Added to wishlist.']);
    }
}
