<?php

namespace App\Modules\Procurement\Repositories;

use App\Modules\Procurement\Models\GoodsReceiptItem;
use Illuminate\Database\Eloquent\Collection;

/**
 * Deterministic hydration: only lines whose POI matches receipt PO and is not soft-deleted, not quarantined.
 */
class GoodsReceiptHydrationRepository
{
    /**
     * @return Collection<int, GoodsReceiptItem>
     */
    public function loadHydratableLines(
        int $tenantId,
        int $goodsReceiptId,
        int $expectedPurchaseOrderId,
    ): Collection {
        return GoodsReceiptItem::withoutGlobalScopes()
            ->where('tenant_id', $tenantId)
            ->where('goods_receipt_id', $goodsReceiptId)
            ->whereNull('quarantined_at')
            ->whereHas('purchaseOrderItem', function ($q) use ($expectedPurchaseOrderId, $tenantId): void {
                $q->withoutGlobalScopes()
                    ->where('tenant_id', $tenantId)
                    ->where('purchase_order_id', $expectedPurchaseOrderId)
                    ->whereNull('deleted_at');
            })
            ->orderBy('id')
            ->get();
    }
}
