<?php

namespace Shm\ShmAdmin\AdminRPC;

use Shm\Shm;
use Shm\ShmAdmin\AdminPanel;
use Shm\ShmAuth\Auth;
use Shm\ShmRPC\ShmRPC;
use Shm\ShmTypes\StructureType;

class AdminRPCMenu
{
    public static function rpc()
    {

        return ShmRPC::lazy(function () {

            $menuItemType = Shm::structure([
                'label' => Shm::string(),
                'icon' => Shm::string(),
                'key' => Shm::string(),
                "collection" => Shm::string(),
                "single" => Shm::boolean(),
                "children" => Shm::arrayOf(
                    Shm::structure([
                        'label' => Shm::string(),
                        'icon' => Shm::string(),
                        'key' => Shm::string(),
                        "collection" => Shm::string(),

                        "single" => Shm::boolean(),
                    ])->staticBaseTypeName("MenuItemChild")
                ),
            ])->staticBaseTypeName("MenuItem");

            return [
                'type' => Shm::structure([
                    'menu' => Shm::arrayOf($menuItemType),
                    'allItems' => Shm::arrayOf($menuItemType),
                    'badges' => Shm::structure([
                        "*" => Shm::mixed()
                    ])->staticBaseTypeName("Badges")
                ]),
                'resolve' => function ($root, $args) {

                    Auth::authenticateOrThrow(...AdminPanel::$authStructures);

                    $initData = AdminPanel::fullSchema();

                    $menu = [];
                    $allItems = [];

                    $badges = [];

                    foreach ($initData->items as $item) {
                        if ($item->type == 'adminGroup') {
                            $_item = [
                                'label' => $item->title,
                                'icon' => $item->assets['icon'] ?? null,
                                'key' => $item->key,
                                "single" => $item->single,
                                'children' => [],
                            ];


                            foreach ($item->items as $subItem) {
                                if ($subItem instanceof StructureType && $subItem->hasBadge()) {
                                    $badges[$subItem->key] = $subItem->getBadge();
                                }

                                $allItems[] = [
                                    'label' => $subItem->title,
                                    'icon' => $subItem->assets['icon'] ?? null,
                                    'key' => $subItem->key,
                                    "single" => $subItem->single,
                                    "collection" => $subItem->collection ?? null,
                                ];

                                $_item['children'][] = [
                                    'label' => $subItem->title,
                                    'icon' => $subItem->assets['icon'] ?? null,
                                    'key' => $subItem->key,
                                    "single" => $subItem->single,
                                    "collection" => $subItem->collection ?? null,
                                ];
                            }

                            $menu[] = $_item;
                        } else {
                            if ($item instanceof StructureType && $item->hasBadge()) {
                                $badges[$item->key] = $item->getBadge();
                            }

                            $menu[] = [
                                'label' => $item->title,
                                'icon' => $item->assets['icon'] ?? null,
                                'key' => $item->key,
                                "single" => $item->single,
                                "collection" => $item->collection ?? null,
                            ];

                            $allItems[] = [
                                'label' => $item->title,
                                'icon' => $item->assets['icon'] ?? null,
                                'key' => $item->key,
                                "single" => $item->single,
                                "collection" => $item->collection ?? null,
                            ];
                        }
                    }

                    return [
                        'menu' => $menu,
                        'badges' => $badges,
                        'allItems' => $allItems,
                    ];
                }
            ];
        });
    }
}