<?php

namespace Modules\Region\Services;

use Illuminate\Http\Request;
use Modules\Region\Interfaces\RegionRepositoryInterface;
use Modules\Region\Models\Region;
use Modules\MainService\Views\ViewFactory;

class RegionService
{
    public function __construct(
        private RegionRepositoryInterface $regionRepository,
        private ViewFactory $viewFactory
    ){}

    public function renderView($request = [])
    {
        $request['regions'] = $this->get($request);
        return $this->render($request);
    }

    public function render($request = [])
    {
        return $this->viewFactory
            ->render($request['view'])
            ->renderView($request);
    }

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

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

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

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

        if (@$request['date_to'])
        {
            $builder->whereDate('regions.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','ASC');
        }

        if (@$request['paginate']) {
            $regions= $builder->paginate(50);
        }else{
            $regions=$builder->get();
        }
        return $regions;
    }
    public function checkItem($id)
    {
        $region = $this->regionRepository->checkItem($id);
        return $region;
    }

    public function getItem($id)
    {
        $region = $this->regionRepository->getItem($id);

        return $region;
    }

    public function getItemTrashed($id)
    {
        $region = $this->regionRepository->getItemTrashed($id);
        return $region;
    }

    public function create(array $request): Region
    {
        $region = $this->regionRepository->create($request);
        $this->regionRepository->saveData($request,$region->id);
        return $region;
    }
    public function update(array $request,int $id): Region
    {
        $region = $this->checkItem($id);
        $region = $this->regionRepository->update($request,$region);
        if(@$request['title_en'])
        {
            $this->regionRepository->saveData($request,$region->id);
        }
        return $region;
    }

    public function updateLock($id)
    {
        $region = $this->checkItem($id);
        $this->update(['lock'=>($region->lock?0:1)],$id);
    }

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