<?php

namespace App\Services\Navigation;

use App\Models\NavItem;
use App\Models\RoleNavItem;
use Illuminate\Support\Facades\Cache;

class NavbarService
{
    public function getMenuForRoles(array $roles): array
    {
        $roles = array_values(array_filter(array_map(fn ($r) => strtolower(trim((string) $r)), $roles)));
        $cacheKey = 'navbar_' . md5(json_encode($roles));

        return Cache::remember($cacheKey, 300, function () use ($roles) {
            $allowedIds = RoleNavItem::getNavItemIdsForRoles($roles);
            if (empty($allowedIds)) {
                return [];
            }

            $rows = NavItem::query()
                ->where('is_enabled', 1)
                ->orderBy('sort_order', 'asc')
                ->get()
                ->toArray();

            $byId = [];
            foreach ($rows as $row) {
                if (in_array((int) $row['id'], $allowedIds, true)) {
                    $row['children'] = [];
                    $byId[$row['id']] = $row;
                }
            }

            $tree = [];
            foreach ($byId as $id => &$node) {
                $pid = $node['menu_parent_id'] ?? null;
                if ($pid && isset($byId[$pid])) {
                    $byId[$pid]['children'][] = &$node;
                } else {
                    $tree[] = &$node;
                }
            }
            unset($node);

            return $tree;
        });
    }

    public function clearCache(): void
    {
        Cache::flush();
    }
}
