<?php

namespace Modules\Rate\Services;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
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_id'])
        {
            $builder->where('product_id', $request['product_id']);
        }

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

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

        if (@$request['store_id'])
        {
            $builder->whereHas('product', function ($query) use($request){
                $query->where('store_id',$request['store_id']);
            });
        }

        if (@$request['warehouse_id'])
        {
            $builder->whereHas('product.warehouses', function ($query) use($request){
                $query->where('warehouse_id',$request['warehouse_id']);
            });
        }

        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->get();
        }
        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);
        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']);
        }
    }
}
