<?php

namespace App\Http\Controllers\Web;

use App\Http\Requests\Web\FavoriteRequest;
use App\Models\Product;
use App\Services\Product\Favorite\FavoriteService;
use App\Services\Product\ProductService;
use App\Services\Response\ResponseService;
use Illuminate\View\View;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

class FavoriteController extends Controller
{
    private $view = 'web.pages.products.';

    public function index(): View
    {
        $title = __('web.favorite');
        $products = (new FavoriteService())->favorites()['data'];
        $fav = true;
        return view($this->view.'products', get_defined_vars());
    }

    public function store(FavoriteRequest $request) :JsonResponse
    {
        $product = Product::find($request->product_id);
        if(!$product)
        {
            return ResponseService::notFound();
        }
        $favorites = (new FavoriteService($product))->favoriteToggle();
        return $favorites;
    }
}
