<?php

namespace App\Services\Repositories;

use App\Models\Slider;
use App\Models\SliderDescription;
use App\Services\Interfaces\MemberRepositoryInterface;
use App\Services\Interfaces\SliderRepositoryInterface;
use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class SliderRepository implements SliderRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Slider::orderBy('id', 'DESC')
            ->join('slider_descriptions AS sd', 'sliders.id', 'sd.slider_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.name', 'sliders.*']);

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

        if ($request->date_to) {
            $query->whereDate('sliders.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(Slider $slider)
    {
        $query = SliderDescription::where('slider_id', $slider->id)
            ->join('languages', 'languages.id', 'slider_descriptions.language_id')
            ->select(['slider_descriptions.*', 'languages.local']);

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

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

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

            SliderDescription::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= Slider::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= Slider::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= Slider::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;
        Slider::whereIn('id', $ids)->onlyTrashed()->restore();
        return [['message'=> __('dashboard.saveDone')]];
    }
}
