<?php

namespace App\Models;

use App\Core\Model;

class MenuItem extends Model
{
    protected static string $table = 'iw_menu_items';

    /**
     * Get all items for a given menu (incl. hidden), sorted by sort_order.
     * Used in the admin backend where visibility should not be filtered.
     */
    public static function getByMenuId(int $menuId): array
    {
        return static::db()->fetchAll(
            "SELECT * FROM iw_menu_items WHERE menu_id = :menu_id ORDER BY sort_order ASC",
            ['menu_id' => $menuId]
        );
    }

    /**
     * Get the next sort_order value for a given menu.
     */
    public static function nextSortOrder(int $menuId): int
    {
        $max = static::db()->fetchColumn(
            "SELECT COALESCE(MAX(sort_order), -1) FROM iw_menu_items WHERE menu_id = :menu_id",
            ['menu_id' => $menuId]
        );
        return (int) $max + 1;
    }
}
