<?php

namespace Modules\Country\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Country\Interfaces\CountryRepositoryInterface;
use Modules\Country\Models\Country;
use Modules\Country\Models\CountryDescription;

class CountryRepository implements CountryRepositoryInterface
{
    public function get(array $request)
    {
        $builder = Country::with(['regions.cities'])->orderBy('id', 'DESC')
            ->join('country_descriptions AS sd', 'countries.id', 'sd.country_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'countries.*']);
        return $builder;
    }

    public function getItem(int $id)
    {
        $country = Country::with(['regions.cities'])->find($id);

        $query = CountryDescription::where('country_id', $country->id)
            ->join('countries', 'country_descriptions.country_id','countries.id')
            ->join('languages', 'languages.id', 'country_descriptions.language_id')
            ->select(['countries.*','country_descriptions.title', 'languages.local']);

        $countryDescription= $query->get();
        // dd($countryDescription->toArray());
        foreach ($countryDescription as $key=>$row) {

            $country[$row->local] = $row;
        }
        return $country;
    }

    public function checkItem(int $id)
    {
        return Country::find($id);
    }

    public function getItemTrashed(int $id)
    {
        $country = Country::onlyTrashed()->find($id);
        return $country ;
    }

    public function create(array $request): Country
    {
        $country = Country::create($request);
        return $country;
    }

    public function update(array $request, ?Country $country): Country
    {
        $country->update($request);

        return $country;
    }

    public function saveData(array $request, int $country_id ) :void
    {
        CountryDescription::where('country_id', $country_id)->delete();
        foreach (languages() as $lang) {
            $data=[
                'country_id'=> $country_id,
                'language_id'=> $lang->id,
                'title'=> $request['title_'.$lang->local],
            ];
            CountryDescription::create($data);
        }
    }
    /**
     * Remove the specified country from storage.
     * @param Country $country
     * @return void
     */
    public function destroy(Country $country) :void
    {
        $country->delete();
    }

    /**
     * Remove several countries by IDs.
     *
     * @param array $request
     * @return void
     */
    public function destroyAll(array $ids) :void
    {
         Country::whereIn('id', $ids)->delete();
    }

    /**
     * Restore the specified country from storage.
     *
     * @param Country $country
     * @return void
     */
    public function restore(Country $country) :void
    {
        $country->restore();
    }

    /**
     * Restore several countries by IDs.
     *
     * @param array $request
     * @return void
     */
    public function restoreAll(array $ids) :void
    {
        Country::whereIn('id', $ids)->onlyTrashed()->restore();
    }
}
