<?php

namespace Modules\Country\Services;

use Illuminate\Http\Request;
use Modules\Country\Interfaces\CountryRepositoryInterface;
use Modules\Country\Models\Country;

class CountryService
{
    private CountryRepositoryInterface $countryRepository ;

    public function __construct(CountryRepositoryInterface $countryRepository)
    {
        $this->countryRepository = $countryRepository;
    }

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

        if (@$request['title'])
        {
            $builder->where('sd.title', 'LIKE', '%'.$request['title'].'%');
        }
        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','DESC');
        }

        if (@$request['paginate']) {
            $countries= $builder->paginate(50);
        }else{
            $countries=$builder->cursor();
        }
        return $countries;
    }
    public function checkItem($id)
    {
        $country = $this->countryRepository->checkItem($id);
        return $country;
    }

    public function getItem($id)
    {
        $country = $this->checkItem($id);
        $countryDescription = $this->countryRepository->getItem($id);
        foreach ($countryDescription as $row) {
            $country[$row->local]= $row;
        }
        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);
        $this->countryRepository->saveData($request,$country->id);
        return $country;
    }
    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']);
        }
    }
}
