<?php

namespace App\Services\Repositories;

use App\Models\Group;
use App\Models\GroupDescription;
use App\Services\Integration\Integration\Connection;
use App\Services\Interfaces\GroupRepositoryInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class GroupRepository implements GroupRepositoryInterface
{
    public function get(Request $request)
    {
        $query = Group::orderBy('order', 'ASC')
            ->join('group_descriptions AS pd', 'groups.id', 'pd.group_id')
            ->join('languages', 'languages.id', 'pd.language_id')
            ->where('local', LaravelLocalization::getCurrentLocale())
            ->select(['pd.name', 'groups.*']);

        if ($request->group_id)
        {
            $query->where('parent_foodics_id', $request->group_id);
        }
        if ($request->ids)
        {
            $query->whereIn('foodics_id', $request->ids);
        }

        if ($request->parents_only ?? null)
        {
            $query->whereNull('parent_foodics_id');
        }

        if ($request->has('active'))
        {
            $query->where('active',$request->active);
        }

        if ( !is_null($request->status) && $request->status == 0) {
            $query->onlyTrashed();
        }
        if ($request->paginate ?? null) {
            $groups= $query->paginate(50);
        }else{
            $groups=$query->get();
        }

        return $groups;
    }

    public function checkItem($id)
    {
        $group = Group::withTrashed()->where('foodics_id', $id)->first();
        return $group;
    }

    public function getItem($id)
    {
        $group = Group::where('foodics_id', $id)->firstOrFail();
        $query = Group::where('groups.foodics_id', $id)
            ->join('group_descriptions', 'group_descriptions.group_id','groups.id')
            ->join('languages', 'languages.id', 'group_descriptions.language_id')
            ->select(['groups.*','group_descriptions.name', 'languages.local']);

        $groupDescription= $query->get();

        foreach ($groupDescription as $key=>$row) {
            if ($row->local == getCurrentLocale())
            {
                $group = $row;
            }
            $group[$row->local] = $row;
        }
        return $group ;
    }

    public function create($request)
    {
        $data = $request instanceof Request ?  $request->all() : (array) $request;
        unset($data['created_at']);
        unset($data['updated_at']);
        unset($data['deleted_at']);
        $data['body'] = json_encode($data);
        $data['foodics_id'] = $data['id'];
        $group = Group::create($data);
        foreach (languages() as $lang) {
            $content=[
                'group_id'=> $group->id,
                'language_id'=> $lang->id,
                'name'=> $lang->local == 'en' ?  $data['name'] : (is_null($data['name_localized']) ? $data['name'] : $data['name_localized']) ,
            ];

            GroupDescription::create($content);
        }
    }
    public function update($request,$id)
    {
        $group = Group::withTrashed()->where('foodics_id', $id)->firstOrFail();
        $data = $request instanceof Request ?  $request->all() : (array) $request;

        unset($data['created_at']);
        unset($data['updated_at']);
        unset($data['deleted_at']);
        $data['body'] = json_encode($data);
        $data['foodics_id'] = $data['id'];
        $group->update($data);
        GroupDescription::where('group_id',$group->id)->delete();
        foreach (languages() as $lang) {
            $content=[
                'group_id'=> $group->id,
                'language_id'=> $lang->id,
                'name'=> $lang->local == 'en' ?  $data['name'] : (is_null($data['name_localized']) ? $data['name'] : $data['name_localized']) ,
            ];
            GroupDescription::create($content);
        }
    }
    //
    public function updateParentGroup(string $id, string $parentId)
    {
        Group::withTrashed()->where('foodics_id', $id)->update(['parent_foodics_id'=> $parentId]);
    }
    //
    public function toggleStatus(int $id)
    {
        $group= Group::withTrashed()->find($id);
        if($group){
            $group->active= $group->active == 1 ? 0 : 1;
            $group->save();
            return [['message'=> __('dashboard.saveDone')], 200];
        }
        return [ ['message'=> __('dashboard.noResult')], 400];
    }
    public function updateOrder($request,$group)
    {
        $other_group = $group->parent_foodics_id ? Group::where('parent_foodics_id',$group->parent_foodics_id) : Group::whereNull('parent_foodics_id') ;
        $other_group = $other_group->where('order',$request->order)->first();
        if($other_group)
        {
            if($group->order)
            {
                $other_group->update(['order'=>$group->order]);
            }
            else
            {
                $last_group = $group->parent_foodics_id ? Group::where('parent_foodics_id',$group->parent_foodics_id) : Group::whereNull('parent_foodics_id') ;
                $last_group = $last_group->orderBy('order','DESC')->first();
                $other_group->update(['order'=>$last_group->order+1]);
            }
        }
        $group->update(['order'=>$request->order]);
    }
    //
    public function delete($request,$id)
    {
        $group = Group::where('foodics_id', $id)->firstOrFail();
        $group->delete();
    }
    public function restore($request,$id)
    {
        $group = Group::onlyTrashed()->where('foodics_id', $id)->first();
        if($group)
        {
            $group->restore();
        }
    }

}
