<?php

namespace App\Services\Repositories;

use App\Models\Cobon;
use App\Models\CityDescription;
use App\Services\Interfaces\CityRepositoryInterface;
use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

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

        if ($request->name) {
            $query->where('cd.name', 'LIKE', '%'.$request->name.'%');
        }
        if ($request->area_id) {
            $query->where('cities.area_id', $request->area_id);
        }
        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) {
            $clients= $query->paginate(50);
        }else{
            $clients=$query->cursor();
        }

        return $clients;
    }

    public function getItem(Cobon $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 get_defined_vars() ;
    }

    //
    public function save(Request $request, ?Cobon $city): Cobon
    {
        $data= $request->all();
        unset($data['id']);// for security reasons
        if (!$city) {
            $city = Cobon::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,
                'name'=> $requestData['name_'.$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
    {
        $client= Cobon::withTrashed()->find($id);
        if ($client) {
            if ($client->deleted_at) {
                $client->forceDelete();
            } else {
                $client->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= Cobon::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
    {
        $client= Cobon::withTrashed()->find($id);
        if ($client) {
            $client->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;
        Cobon::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }
}
