<?php

namespace Modules\City\Services;

use Illuminate\Http\Request;
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['region_id']) {
            $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);
        } elseif (@$request['api']) {
            $cities = $builder;
        } else {
            $cities = $builder->cursor();
        }
        return $cities;
    }
    public function checkItem($id)
    {
        $city = $this->cityRepository->checkItem($id);
        return $city;
    }

    public function getItem($id)
    {
        $city = $this->checkItem($id);
        $cityDescription = $this->cityRepository->getItem($id);
        foreach ($cityDescription as $row) {
            $city[$row->local] = $row;
        }
        return $city;
    }

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

    public function create(array $request): City
    {
        $city = $this->cityRepository->create($request);
        $this->cityRepository->saveData($request, $city->id);
        return $city;
    }
    public function update(array $request, int $id): City
    {
        $city = $this->checkItem($id);


        $city = $this->cityRepository->update($request, $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']);
        }
    }
}
