<?php

namespace Modules\Area\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Area\Interfaces\AreaRepositoryInterface;
use Modules\Area\Models\Area;
use Modules\Area\Models\AreaDescription;

class AreaRepository implements AreaRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Area::orderBy('id', 'DESC')
            ->join('area_descriptions AS sd', 'areas.id', 'sd.area_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'areas.*']);
        if ($request->city_id)
        {
            $query->where('areas.city_id',$request->city_id);
        }

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

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


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

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

        return $areas;
    }

    public function getItem(Area $area)
    {
        $query = AreaDescription::where('area_id', $area->id)
            ->join('languages', 'languages.id', 'area_descriptions.language_id')
            ->select(['area_descriptions.*', 'languages.local']);

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

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

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

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