<?php

namespace Modules\City\Services;

use Illuminate\Http\Request;
use Modules\City\Enums\CityBoolEnum;
use Modules\City\Interfaces\CityRepositoryInterface;
use Modules\City\Models\City;

class CityService
{
    private CityRepositoryInterface $cityRepository ;

    public function __construct(CityRepositoryInterface $cityRepository)
    {
        $this->cityRepository = $cityRepository;
    }

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

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

        if (@$request['ids'])
        {
            $builder->whereIn('cities.id',$request['ids']);
        }

        if (@$request['region_id'])
        {
            if (is_array($request['region_id']))
            {
                $builder->whereIn('cities.region_id',  $request['region_id']);
            }
            else
            {
                $builder->where('cities.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('cities.created_at', '>=', $request['date_from']);
        }

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

    public function getItem($id)
    {
        $city = $this->cityRepository->getItem($id);

        return $city;
    }

    public function getItemTrashed($id)
    {
        $city = $this->cityRepository->getItemTrashed($id);
        return $city;
    }

    public function create(array $request): City
    {
        // if($request['default_city'] == CityBoolEnum::TRUE->value)
        // {
        //     $this->unDefaultCities();
        // }
        $city = $this->cityRepository->create([]);
        $this->cityRepository->saveData($request,$city->id);
        return $city;
    }
    public function update(array $request,int $id): City
    {
        // if($request['default_city'] == CityBoolEnum::TRUE->value)
        // {
        //     $this->unDefaultCities();
        // }
        $city = $this->checkItem($id);


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

    public function unDefaultCities()
    {
        $city = $this->defaultCity();
        if ($city)
        {
            $this->cityRepository->unDefaultCities($city);
        }
    }

    public function defaultCity()
    {
        return $this->cityRepository->defaultCity();
    }
}
