<?php

namespace Modules\Way\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Way\Interfaces\WayRepositoryInterface;
use Modules\Way\Models\Way;
use Modules\Way\Models\WayDescription;

class WayRepository implements WayRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Way::orderBy('id', 'DESC')
            ->join('way_descriptions AS sd', 'ways.id', 'sd.way_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'ways.*']);

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

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


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

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

        return $ways;
    }

    public function getItem(Way $way)
    {
        $query = WayDescription::where('way_id', $way->id)
            ->join('ways', 'ways.id', 'way_descriptions.way_id')
            ->join('languages', 'languages.id', 'way_descriptions.language_id')
            ->select(['ways.*','way_descriptions.title', 'languages.local']);

        $wayDescription= $query->get();
        foreach ($wayDescription as $key=>$row) {
            if ($row->local == getCurrentLocale())
            {
                $way = $row;
            }
            $way[$row->local] = $row;
        }
        return $way ;
    }

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

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

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

