<?php

namespace Modules\Country\Services;

use Illuminate\Http\Request;
use Modules\Country\Interfaces\CountryRepositoryInterface;
use Modules\Country\Models\Country;
use Modules\MainService\Views\ViewFactory;

class CountryService
{
    public function __construct(
        private CountryRepositoryInterface $countryRepository,
        private ViewFactory $viewFactory
    ){}

    public function renderView($request = [])
    {
        $request['countries'] = $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->countryRepository->get($request);

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

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

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

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

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

    public function getItem($id)
    {
        $country = $this->countryRepository->getItem($id);

        return $country;
    }

    public function getItemTrashed($id)
    {
        $country = $this->countryRepository->getItemTrashed($id);
        return $country;
    }

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

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

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