<?php

namespace App\Http\Controllers\Api\V1\Customer;

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

class WishlistController extends Controller
{
    public function index()
    {
        $wishlist = auth()->user()->wishlists()
            ->with(['product.images'])
            ->latest()
            ->get();
        return response()->json($wishlist);
    }

    public function store(Request $request)
    {
        $request->validate([
            'product_id' => 'required|exists:products,id'
        ]);

        $userId = auth()->id();

        $wishlist = Wishlist::firstOrCreate([
            'user_id' => $userId,
            'product_id' => $request->product_id
        ]);

        return response()->json([
            'success' => true,
            'message' => 'Product added to wishlist.',
            'wishlist' => $wishlist
        ]);
    }

    public function destroy($productId)
    {
        $userId = auth()->id();
        Wishlist::where('user_id', $userId)->where('product_id', $productId)->delete();

        return response()->json([
            'success' => true,
            'message' => 'Product removed from wishlist.'
        ]);
    }
}
