<?php

namespace Tests\Unit\Services\Navigation;

use App\Services\Navigation\NavbarService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class NavbarServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_get_menu_for_roles_builds_tree(): void
    {
        $roleId = DB::table('roles')->insertGetId([
            'name' => 'admin',
            'priority' => 1,
        ]);

        $parentId = DB::table('nav_items')->insertGetId([
            'label' => 'Parent',
            'url' => 'parent',
            'sort_order' => 1,
            'is_enabled' => 1,
        ]);

        $childId = DB::table('nav_items')->insertGetId([
            'label' => 'Child',
            'url' => 'child',
            'menu_parent_id' => $parentId,
            'sort_order' => 2,
            'is_enabled' => 1,
        ]);

        DB::table('role_nav_items')->insert([
            ['role_id' => $roleId, 'nav_item_id' => $parentId],
            ['role_id' => $roleId, 'nav_item_id' => $childId],
        ]);

        $service = new NavbarService();
        $menu = $service->getMenuForRoles(['admin']);

        $this->assertCount(1, $menu);
        $this->assertSame('Parent', $menu[0]['label']);
        $this->assertCount(1, $menu[0]['children']);
        $this->assertSame('Child', $menu[0]['children'][0]['label']);
    }
}
