<?php

namespace Tests\Unit\Models;

use App\Models\Menu;
use Tests\TestCase;

class MenuTest extends TestCase
{
    /** @test */
    public function it_can_create_a_menu_with_factory(): void
    {
        $menu = Menu::factory()->create();

        $this->assertInstanceOf(Menu::class, $menu);
        $this->assertIsString($menu->name);
        $this->assertIsString($menu->url);
    }

    /** @test */
    public function it_has_correct_fillable_attributes(): void
    {
        $menuData = [
            'name' => 'Home',
            'url' => '/home',
            'new_tab' => false,
            'parent' => null,
        ];

        $menu = Menu::query()->create($menuData);

        foreach ($menuData as $key => $value) {
            $this->assertEquals($value, $menu->$key);
        }
    }

    /** @test */
    public function it_orders_menus_by_name(): void
    {
        $menu1 = Menu::factory()->create(['name' => 'Zebra']);
        $menu2 = Menu::factory()->create(['name' => 'Alpha']);
        $menu3 = Menu::factory()->create(['name' => 'Beta']);

        $menus = Menu::query()->orderBy('name')->get();

        $this->assertEquals($menu2->id, $menus[0]->id);
        $this->assertEquals($menu3->id, $menus[1]->id);
        $this->assertEquals($menu1->id, $menus[2]->id);
    }

    /** @test */
    public function it_can_handle_parent_child_relationships(): void
    {
        Menu::factory()->create(['parent' => null]);
        $childMenu = Menu::factory()->create(['parent' => 'home']);

        $this->assertEquals('home', $childMenu->parent);
    }
}
