<?php

namespace App\Http\Controllers;

use App\Enums\DbStatusFieldEnum;
use App\Enums\ProductTypeEnum;
use App\Models\ProductGroup;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\View\View;

class TestController extends Controller
{
    private function parseNodes($parentNode, $collection)
    {
        $parentNodeName = $parentNode->name;
        $rootNode = false;
        if (! isset($parentNode->parent_id) || is_null($parentNode->parent_id)) {
            // o maga a root Node
            $rootNode = true;
        }

        foreach ($collection as $row) {
            if (isset($row[$parentNodeName])
                && strlen($row[$parentNodeName]) > 0
                && $row[$parentNodeName] !== 'n/a'
            ) {

                $node = [
                    'name' => $row[$parentNodeName],
                    'status' => DbStatusFieldEnum::active,

                ];
                if ($rootNode) {
                    if (ProductTypeEnum::hasValue($node['name'])) {
                        $node['type'] = ProductTypeEnum::getKey($node['name']);
                    } else {
                        $node['type'] = ProductTypeEnum::X();
                    }
                } else {
                    $node['type'] = $parentNode->type;

                }
                $treeNode = $parentNode->children()->create($node);
                $this->parseNodes($treeNode, $collection);
            }
        }

    }

    public function test1(): Response|JsonResponse
    {
        // ...
        return response('helloka');
    }

    public function test2(): Response|JsonResponse
    {
        $o = new ProductGroup;

        return response()->json($o->allToTreeArray()['children']);
    }

    public function test(): View|Response|JsonResponse
    {
        // ...
        return view('test.test');
    }
}
