<?php

namespace Modules\Reason\Services;

use Illuminate\Http\Request;
use Modules\Reason\Interfaces\ReasonRepositoryInterface;
use Modules\Reason\Models\Reason;

class ReasonService
{
    private ReasonRepositoryInterface $reasonRepository ;

    public function __construct(ReasonRepositoryInterface $reasonRepository)
    {
        $this->reasonRepository = $reasonRepository;
    }

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

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

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

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

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

        if (@$request['date_to'])
        {
            $builder->whereDate('reasons.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']) {
            $reasons= $builder->paginate(50);
        }else{
            $reasons=$builder->cursor();
        }
        return $reasons;
    }
    public function checkItem($id)
    {
        $reason = $this->reasonRepository->checkItem($id);
        return $reason;
    }

    public function getItem($id)
    {
        $reason = $this->reasonRepository->getItem($id);

        return $reason;
    }

    public function getItemTrashed($id)
    {
        $reason = $this->reasonRepository->getItemTrashed($id);
        return $reason;
    }

    public function create(array $request): Reason
    {
        $reason = $this->reasonRepository->create($request);
        $this->reasonRepository->saveData($request,$reason->id);
        return $reason;
    }
    public function update(array $request,int $id): Reason
    {
        $reason = $this->checkItem($id);


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