<?php

declare(strict_types=1);

namespace Maestro\Core\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class NavigationNode extends Model
{
    protected $table = 'maestro_navigation_nodes';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'parent_id',
        'module_id',
        'sort_order',
        'key',
        'label',
        'icon',
        'route',
        'permission',
        'is_header',
        'pinned_bottom',
        'description',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'is_header' => 'boolean',
            'pinned_bottom' => 'boolean',
        ];
    }

    /**
     * @return BelongsTo<NavigationNode, $this>
     */
    public function parent(): BelongsTo
    {
        return $this->belongsTo(self::class, 'parent_id');
    }

    /**
     * @return HasMany<NavigationNode, $this>
     */
    public function children(): HasMany
    {
        return $this->hasMany(self::class, 'parent_id')->orderBy('sort_order');
    }

    /**
     * @return BelongsTo<MaestroModule, $this>
     */
    public function module(): BelongsTo
    {
        return $this->belongsTo(MaestroModule::class, 'module_id');
    }
}
