<?php

namespace App\Http\Controllers\WebHook;

use App\Models\Room;
use App\Services\Interfaces\GroupRepositoryInterface;
use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\View\View;

class GroupController extends Controller
{
    private GroupRepositoryInterface $groupRepository ;


    public function __construct(
        GroupRepositoryInterface $groupRepository,
    )
    {
        $this->groupRepository = $groupRepository;
    }

    public function create(Request $request)
    {
        $checkItem = $this->groupRepository->checkItem($request->id);

        if ($checkItem)
        {
            // $request->parent_foodics_id = null;
            $this->groupRepository->update($request,$request->id);
        }
        else
        {
            $this->groupRepository->create($request);
        }
        
        foreach ($request->subgroups ?? [] as $subGroup)
        {
            $checkItem = $this->groupRepository->checkItem($subGroup['id']);
            if ($checkItem)
            {
                $this->groupRepository->updateParentGroup($subGroup['id'], $request->id);
            }
        }

        $response = [
            'status'=>Response::HTTP_OK,
            'message'=>'success',
            'data'=>[]
        ]; 
        return ResponseService::response($response);
    }
    public function update(Request $request,$id)
    {
        $checkItem = $this->groupRepository->checkItem($request->id);

        if ($checkItem)
        {
            // $request->parent_foodics_id = null;
            $this->groupRepository->update($request,$request->id);
        }
        else
        {
            $this->groupRepository->create($request);
        }
        
        foreach ($request->subgroups ?? [] as $subGroup)
        {
            $checkItem = $this->groupRepository->checkItem($subGroup['id']);
            if ($checkItem)
            {
                $this->groupRepository->updateParentGroup($subGroup['id'], $request->id);
            }
        }

        $response = [
            'status'=>Response::HTTP_OK,
            'message'=>'success',
            'data'=>[]
        ];
        return ResponseService::response($response);
    }
    public function restore(Request $request,$id)
    {
        $this->groupRepository->restore($request,$id);
        $response = [
            'status'=>Response::HTTP_OK,
            'message'=>'success',
            'data'=>[]
        ];
        return ResponseService::response($response);
    }
    public function destroy(Request $request,$id)
    {
        $this->groupRepository->delete($request,$id);
        $response = [
            'status'=>Response::HTTP_OK,
            'message'=>'success',
            'data'=>[]
        ];
        return ResponseService::response($response);
    }


}
