<?php

namespace Modules\Option\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Option\Interfaces\OptionRepositoryInterface;
use Modules\Option\Models\Option;
use Modules\Option\Models\OptionDescription;

class OptionRepository implements OptionRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Option::orderBy('id', 'DESC')
            ->join('option_descriptions AS sd', 'options.id', 'sd.option_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'options.*']);

        if ($request->title) {
            $query->where('sd.title', 'LIKE', '%'.$request->title.'%');
        }
        if (!is_null($request->type)) {
            $query->where('options.status', $request->type);
        }
        if ($request->date_from) {
            $query->whereDate('options.created_at', '>=', $request->date_from);
        }

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


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

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

        return $options;
    }

    public function getItem(Option $option)
    {
        $query = OptionDescription::where('option_id', $option->id)
            ->join('languages', 'languages.id', 'option_descriptions.language_id')
            ->select(['option_descriptions.*', 'languages.local']);

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

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

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

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