<?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(Request $request)
    {
        $query = Country::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.*']);

        if ($request->title) {
            $query->where('sd.title', 'LIKE', '%'.$request->title.'%');
        }
        if ($request->date_from) {
            $query->whereDate('countries.created_at', '>=', $request->date_from);
        }

        if ($request->date_to) {
            $query->whereDate('countries.created_at', '<=', $request->date_to);
        }


        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }

        if ($request->paginate ?? null) {
            $countries= $query->paginate(50);
        }else{
            $countries=$query->cursor();
        }

        return $countries;
    }

    public function getItem(Country $country)
    {
        $query = CountryDescription::where('country_id', $country->id)
            ->join('languages', 'languages.id', 'country_descriptions.language_id')
            ->select(['country_descriptions.*', 'languages.local']);

        $countryDescription= $query->get();
        // dd($countryDescription->toArray());
        foreach ($countryDescription as $row) {
            $country[$row->local]= $row;
        }
        return $country ;
    }

    //
    public function save(Request $request, ?Country $country): Country
    {
        $data= $request->all();
        unset($data['id']);// for security reasons
        if (!$country) {
            $country = Country::create($data);
        } else {
            $country->update($data);
            CountryDescription::where('country_id', $country->id)->delete();
        }
        $this->saveData( $request, $country->id );
        return $country;
    }

    private function saveData(Request $request, int $country_id ) :void
    {
        $requestData= $request->all();
        foreach (languages() as $lang) {
            $data=[
                'country_id'=> $country_id,
                'language_id'=> $lang->id,
                'title'=> $requestData['title_'.$lang->local],
            ];

            CountryDescription::create($data);
        }
    }
    /**
     * Remove the specified client from storage.
     * @param Request $request
     * @param int $id
     * @return array
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :array
    {
        $country= Country::withTrashed()->find($id);
        if ($country) {
            if ($country->deleted_at) {
                $country->forceDelete();
            } else {
                $country->delete();
            }
            return [ ['message'=> __('dashboard.deletedDone')]];
        } else{
            return [ ['message'=> __('dashboard.noResult')], 400];
        }
    }

    /**
     * Remove several clients by IDs.
     *
     * @param Request $request
     * @return array
     */
    public function destroyAll(Request $request) :array
    {
        $ids= $request->ids;
        $query= Country::whereIn('id', $ids);
        if ($request->force) {
            $deleted= $query->onlyTrashed()->forceDelete();
        } else {
            $deleted= $query->delete();
        }
        if($deleted){
            return [['message'=> __('dashboard.deletedDone')]];
        }
        return [ ['message'=> __('dashboard.noResult')], 400];
    }

    /**
     * Restore the specified client from storage.
     *
     * @param Request $request
     * @param int $id
     * @return array
     */
    public function restore(Request $request, int $id) :array
    {
        $country= Country::withTrashed()->find($id);
        if ($country) {
            $country->restore();
            return [['message'=> __('dashboard.saveDone')]];
        }
        return [['message'=> __('dashboard.noResult')], 400];

    }

    /**
     * Restore several clients by IDs.
     *
     * @param Request $request
     * @return array
     */
    public function restoreAll(Request $request) :array
    {
        $ids= $request->ids;
        Country::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }
}
