<?php

namespace App\Domains\Forms\Services;

use App\Domains\Catalog\Models\Category;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

class StaffFormService
{
    /** @return array{categories: Collection<int, Category>} */
    public function get(Tenant $tenant): array
    {
        return [
            'categories' => Category::query()
                ->whereNull('categoria_id')
                ->where(function (Builder $query) use ($tenant): void {
                    $query->where('tenant_code', $tenant->codigo)
                        ->orWhereHas('catalogItems', fn (Builder $items) => $items
                            ->where('tenant_code', $tenant->codigo));
                })
                ->orderBy('nombre')
                ->get(),
        ];
    }
}
