<?php

namespace Modules\Region\Repositories;

use Illuminate\Http\Request;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
use Modules\Region\Interfaces\RegionRepositoryInterface;
use Modules\Region\Models\Region;
use Modules\Region\Models\RegionDescription;

class RegionRepository implements RegionRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Region::orderBy('id', 'DESC')
            ->join('region_descriptions AS sd', 'regions.id', 'sd.region_id')
            ->join('languages', 'languages.id', 'sd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['sd.title', 'regions.*']);

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

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


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

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

        return $regions;
    }

    public function getItem(Region $region)
    {
        $query = RegionDescription::where('region_id', $region->id)
            ->join('languages', 'languages.id', 'region_descriptions.language_id')
            ->select(['region_descriptions.*', 'languages.local']);

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

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

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

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