<?php
namespace App\Http\Controllers\API\Customer;

use App\Http\Controllers\Controller;
use App\Models\Wishlist;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CustomerWishlistController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $wishlist = Wishlist::with('product:id,name,price,sale_price,thumbnail,average_rating')
            ->where('user_id', $request->user()->id)->get();
        return response()->json(['wishlist' => $wishlist]);
    }

    public function store(Request $request): JsonResponse
    {
        $data = $request->validate(['product_id' => 'required|exists:products,id']);
        $item = Wishlist::firstOrCreate(['user_id' => $request->user()->id, 'product_id' => $data['product_id']]);
        return response()->json(['message' => 'Added to wishlist.', 'item' => $item], 201);
    }

    public function destroy(Request $request, int $id): JsonResponse
    {
        Wishlist::where('user_id', $request->user()->id)->where('product_id', $id)->delete();
        return response()->json(['message' => 'Removed from wishlist.']);
    }
}
