<?php

namespace App\Models;

class MenuItemModel extends TenantModel
{
    protected $table            = 'menu_items';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'object';
    protected $useTimestamps    = true;

    protected $allowedFields = ['name', 'price', 'category', 'description', 'is_available', 'photo_path'];

    protected $validationRules = [
        'name'  => 'required|max_length[150]',
        'price' => 'required|decimal',
    ];

    public function available()
    {
        return $this->where('is_available', 1);
    }

    /** Grouped by category for the order-entry menu picker. */
    public function groupedByCategory(): array
    {
        $items    = $this->available()->orderBy('category')->orderBy('name')->findAll();
        $grouped  = [];

        foreach ($items as $item) {
            $grouped[$item->category ?: 'Other'][] = $item;
        }

        return $grouped;
    }
}
