<?php

namespace Modules\Area\Services;

use Illuminate\Http\Request;
use Modules\Area\Interfaces\AreaRepositoryInterface;
use Modules\Area\Models\Area;

class AreaService
{
    private AreaRepositoryInterface $areaRepository ;

    public function __construct(AreaRepositoryInterface $areaRepository)
    {
        $this->areaRepository = $areaRepository;
    }

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

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

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

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

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

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

    public function getItem($id)
    {
        $area = $this->checkItem($id);
        $areaDescription = $this->areaRepository->getItem($id);
        foreach ($areaDescription as $row) {
            $area[$row->local]= $row;
        }
        return $area;
    }

    public function getItemTrashed($id)
    {
        $area = $this->areaRepository->getItemTrashed($id);
        return $area;
    }

    public function create(array $request): Area
    {
        $area = $this->areaRepository->create($request);
        $this->areaRepository->saveData($request,$area->id);
        return $area;
    }
    public function update(array $request,int $id): Area
    {
        $area = $this->checkItem($id);


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