<?php

namespace Modules\Rate\Services;

use Illuminate\Http\Request;
use Modules\Rate\Interfaces\RateRepositoryInterface;
use Modules\Rate\Models\Rate;

class RateService
{
    private RateRepositoryInterface $rateRepository ;

    public function __construct(RateRepositoryInterface $rateRepository)
    {
        $this->rateRepository = $rateRepository;
    }

    public function get(array $request)
    {
        $builder = $this->rateRepository->get($request);

        if (@$request['rate'])
        {
            $builder->where('rate', $request['rate']);
        }

        if (@$request['product'])
        {
            $builder->whereHas('product.currentDescription', function ($query) use($request){
                $query->where('title','LIKE', '%'.$request['product'].'%');
            });
        }

        if (@$request['user'])
        {
            $builder->whereHas('user', function ($query) use($request){
                $query->where('name','LIKE', '%'.$request['user'].'%');
            });
        }

        if (@$request['date_from'])
        {
            $builder->whereDate('created_at', '>=', $request['date_from']);
        }

        if (@$request['date_to'])
        {
            $builder->whereDate('created_at', '<=', $request['date_to']);
        }

        if (isset($request['status']))
        {
            if (@$request['status'] == 0)
            {
                $builder->onlyTrashed();
            }
        }

        if (@$request['orderBy'])
        {
            $builder->orderBy($request['orderBy']['key'],$request['orderBy']['value']);
        }
        else
        {
            $builder->orderBy('id','DESC');
        }

        if (@$request['paginate']) {
            $rates= $builder->paginate(50);
        }else{
            $rates=$builder->cursor();
        }
        return $rates;
    }

    public function ratesNumber()
    {
        return [1,2,3,4,5];
    }

    public function checkItem($id)
    {
        $rate = $this->rateRepository->checkItem($id);
        return $rate;
    }

    public function getItem($id)
    {
        $rate = $this->checkItem($id);

        return $rate;
    }

    public function getItemTrashed($id)
    {
        $rate = $this->rateRepository->getItemTrashed($id);
        return $rate;
    }

    public function create(array $request): Rate
    {
        $rate = $this->rateRepository->create($request);
        $this->rateRepository->saveData($request,$rate->id);
        return $rate;
    }
    public function update(array $request,int $id): Rate
    {
        $rate = $this->checkItem($id);


        $rate = $this->rateRepository->update($request,$rate);
        $this->rateRepository->saveData($request,$rate->id);
        return $rate;
    }
    public function destroy(int $id): void
    {
        $rate = $this->checkItem($id);
        if ($rate)
        {
            $this->rateRepository->destroy($rate);
        }
        else
        {
            $rate = $this->getItemTrashed($id);
            $this->rateRepository->restore($rate);
        }
    }
    public function destroyAll(array $request): void
    {
        if ($request['status'] == 1)
        {
            $this->rateRepository->destroyAll($request['ids']);
        }
        else
        {
            $this->rateRepository->restoreAll($request['ids']);
        }
    }
}
