<?php

namespace App\Services;

use App\Models\InventoryProduct;
use Illuminate\Support\Collection;

/**
 * Productos con stock en o por debajo del mínimo efectivo (min_stock o umbral global).
 */
class InventoryLowStockQuery
{
    /**
     * @return Collection<int, InventoryProduct>
     */
    public function collect(string $globalThreshold): Collection
    {
        return InventoryProduct::query()
            ->where('active', true)
            ->orderBy('sku')
            ->get()
            ->filter(function (InventoryProduct $p) use ($globalThreshold): bool {
                $minEffective = $p->min_stock !== null
                    ? (string) $p->min_stock
                    : $globalThreshold;

                return bccomp((string) $p->stock_quantity, $minEffective, 4) <= 0;
            })
            ->values();
    }
}
