<?php

namespace Modules\Slider\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Slider\Interfaces\SliderRepositoryInterface;
use Modules\Slider\Models\Slider;
use Modules\Slider\Models\SliderDescription;

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.title', 'sliders.*']);

        if ($request->title) {
            $query->where('sd.title', 'LIKE', '%'.$request->title.'%');
        }
        if ($request->store_id) {
            $query->where('sliders.store_id', $request->store_id);
        }
        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) {
            $sliders= $query->paginate(50);
        }else{
            $sliders=$query->cursor();
        }

        return $sliders;
    }

    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 $slider ;
    }

    //
    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,
                'title'=> $requestData['title_'.$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
    {
        $slider= Slider::withTrashed()->find($id);
        if ($slider) {
            if ($slider->deleted_at) {
                $slider->forceDelete();
            } else {
                $slider->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
    {
        $slider= Slider::withTrashed()->find($id);
        if ($slider) {
            $slider->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')]];
    }
}
