<?php

namespace Modules\City\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\City\Interfaces\CityRepositoryInterface;
use Modules\City\Models\City;
use Modules\City\Models\CityDescription;

class CityRepository implements CityRepositoryInterface
{
    public function get(Request $request)
    {
        $query = City::orderBy('id', 'DESC')
            ->join('city_descriptions AS sd', 'cities.id', 'sd.city_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'cities.*']);
        if ($request->region_id)
        {
            $query->where('cities.region_id',$request->region_id);
        }

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

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


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

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

        return $cities;
    }

    public function getItem(City $city)
    {
        $query = CityDescription::where('city_id', $city->id)
            ->join('languages', 'languages.id', 'city_descriptions.language_id')
            ->select(['city_descriptions.*', 'languages.local']);

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

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

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

            CityDescription::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
    {
        $city= City::withTrashed()->find($id);
        if ($city) {
            if ($city->deleted_at) {
                $city->forceDelete();
            } else {
                $city->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= City::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
    {
        $city= City::withTrashed()->find($id);
        if ($city) {
            $city->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;
        City::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }
}
