<?php

namespace App\Http\Controllers;

use App\Models\Favorite;
use App\Models\Product;
use Illuminate\Http\Request;

class FavoriteController extends Controller
{
    // তালিকা দেখানো
    public function index(Request $request)
    {
        $user = $request->user();

        // প্রোডাক্টসহ ফেভারিটগুলো
        $favorites = Favorite::with('product')
            ->where('user_id', $user->id)
            ->latest('id')
            ->paginate(24);

        return view('user.favorites.index', compact('favorites'));
    }

    // যোগ করা
    public function store(Request $request, Product $product)
    {
        $user = $request->user();
        Favorite::firstOrCreate([
            'user_id'    => $user->id,
            'product_id' => $product->id,
        ]);

        return back()->with('success', 'ফেভারিটে যোগ হয়েছে।');
    }

    // মুছে ফেলা
    public function destroy(Request $request, Product $product)
    {
        $user = $request->user();

        Favorite::where('user_id', $user->id)
            ->where('product_id', $product->id)
            ->delete();

        return back()->with('success', 'ফেভারিট থেকে মুছে ফেলা হয়েছে।');
    }
}