<?php

namespace Modules\Option\Services;

use Illuminate\Http\Request;
use Modules\Option\Interfaces\OptionRepositoryInterface;
use Modules\Option\Models\Option;

class OptionService
{
    private OptionRepositoryInterface $optionRepository ;

    public function __construct(OptionRepositoryInterface $optionRepository)
    {
        $this->optionRepository = $optionRepository;
    }

    public function get(array $request = [])
    {
        $builder = $this->optionRepository->get($request);

        if (@$request['title'])
        {
            $builder->where('sd.title', 'LIKE', '%'.$request['title'].'%');
        }
        if (@$request['date_from'])
        {
            $builder->whereDate('options.created_at', '>=', $request['date_from']);
        }

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

        if (@$request['attribute_id'])
        {
            $builder->whereHas('attributes', function($q) use ($request){
                $q->where('attribute_id',  $request['attribute_id']);
            });
        }

        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']) {
            $options= $builder->paginate(50);
        }else{
            $options=$builder->cursor();
        }
        return $options;
    }
    public function checkItem($id)
    {
        $option = $this->optionRepository->checkItem($id);
        return $option;
    }

    public function getItem($id)
    {
        $option = $this->checkItem($id);
        $optionDescription = $this->optionRepository->getItem($id);
        foreach ($optionDescription as $row) {
            $option[$row->local]= $row;
        }
        return $option;
    }

    public function getItemTrashed($id)
    {
        $option = $this->optionRepository->getItemTrashed($id);
        return $option;
    }

    public function create(array $request): Option
    {
        $option = $this->optionRepository->create($request);
        $this->optionRepository->saveData($request,$option->id);
        return $option;
    }
    public function update(array $request,int $id): Option
    {
        $option = $this->checkItem($id);


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