<?php

namespace Modules\Product\Http\Controllers;

use App\Functions\ResponseHelper;
use App\Http\Controllers\BasicController;
use Illuminate\Http\Request;
use Modules\Product\Http\Requests\ProductFavouriteRequest;
use Modules\Product\Http\Resources\ProductResource;

class ProductFavouriteController extends BasicController
{
    public function index($lang, Request $request)
    {
        $this->CheckAuth();
        $Products = $this->Client->FavProducts()->distinct('product_id')->where('products.status', 1)->get();

        return ResponseHelper::make(ProductResource::collection($Products));
    }

    public function store($lang, ProductFavouriteRequest $request)
    {
        $this->CheckAuth();
        $this->Client->FavProducts()->attach($request->product_id);

        return ResponseHelper::make(null, __('trans.addedSuccessfully'));
    }

    public function show($lang, $id, Request $request)
    {
        $this->CheckAuth();

        return ResponseHelper::make(ProductResource::make($this->Client->FavProducts()->where('id', $id)->first()));
    }

    public function update($lang, $id, ProductFavouriteRequest $request)
    {
        $this->CheckAuth();
        $this->Client->FavProducts()->where('id', $id) > update($request->validated());

        return ResponseHelper::make(null, __('trans.updatedSuccessfully'));
    }

    public function destroy($lang, $id)
    {
        $this->CheckAuth();
        $this->Client->FavProducts()->detach($id);

        return ResponseHelper::make(null, __('trans.DeletedSuccessfully'));
    }
}
