<?php

namespace Tests\Unit\Services\Navigation;

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

class NavBuilderServiceTest extends TestCase
{
    use RefreshDatabase;

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

        $service = new NavBuilderService(new NavbarService());
        $result = $service->save([
            'label' => 'Dashboard',
            'url' => 'dashboard',
            'sort_order' => 1,
            'is_enabled' => 1,
            'roles' => [$roleId],
        ]);

        $this->assertTrue($result['ok']);
        $this->assertDatabaseHas('nav_items', ['label' => 'Dashboard']);
        $this->assertDatabaseHas('role_nav_items', [
            'role_id' => $roleId,
            'nav_item_id' => $result['id'],
        ]);
    }

    public function test_reorder_updates_sort_order(): void
    {
        $id = DB::table('nav_items')->insertGetId([
            'label' => 'Item',
            'url' => 'item',
            'sort_order' => 1,
            'is_enabled' => 1,
        ]);

        $service = new NavBuilderService(new NavbarService());
        $service->reorder([$id => 5]);

        $this->assertDatabaseHas('nav_items', [
            'id' => $id,
            'sort_order' => 5,
        ]);
    }
}
