<?php

namespace App\Http\Controllers\Panel;

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

class ProductFavoriteController extends Controller
{
    public function index(Request $request)
    {
        $user = auth()->user();

        $favorites = ProductFavorite::where('user_id', $user->id)
            ->with(['product' => function ($q) {
                $q->with(['creator', 'category']);
            }])
            ->orderBy('created_at', 'desc')
            ->paginate(12);

        $data = [
            'pageTitle' => trans('update.product_wishlist'),
            'favorites' => $favorites,
        ];

        return view(getTemplate() . '.panel.store.product_wishlist', $data);
    }

    public function destroy($id)
    {
        $user = auth()->user();

        $favorite = ProductFavorite::where('id', $id)
            ->where('user_id', $user->id)
            ->first();

        if (! empty($favorite)) {
            $favorite->delete();

            return redirect('/panel/product-favorites')->with('success', trans('public.done'));
        }

        abort(404);
    }
}
