<?php

namespace App\Models;

use App\Models\BaseModel;
use Illuminate\Database\Eloquent\SoftDeletes;

class NavItem extends BaseModel
{
    protected $table = 'nav_items';

    // ✅ CI: timestamps + soft deletes
    use SoftDeletes;

    public $timestamps = true;

    protected $fillable = [
        'menu_parent_id',
        'label',
        'url',
        'icon_class',
        'target',
        'sort_order',
        'is_enabled',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected $casts = [
        'menu_parent_id' => 'integer',
        'sort_order'     => 'integer',
        'is_enabled'     => 'boolean',
    ];

    /* Optional relationships */
    public function parent()
    {
        return $this->belongsTo(self::class, 'menu_parent_id');
    }

    public function children()
    {
        return $this->hasMany(self::class, 'menu_parent_id');
    }

    /**
     * Equivalent of CI getChildrenOf()
     * NOTE: CI code uses where('parent_id', ...) but allowedFields has menu_parent_id.
     * This Laravel version uses menu_parent_id (likely correct).
     */
    public static function getChildrenOf(int $parentId): array
    {
        return static::query()
            ->where('menu_parent_id', $parentId)
            ->where('is_enabled', 1)
            ->orderBy('sort_order', 'asc')
            ->get()
            ->toArray();
    }
}