<?php

namespace App\Modules\Sales\Repositories;

use App\Modules\Sales\Models\SalesOrderLine;
use Illuminate\Support\Collection;

class SalesOrderLineRepository
{
    /**
     * @return Collection<int, SalesOrderLine>
     */
    public function lockForOrderOrdered(int $tenantId, int $salesOrderId): Collection
    {
        return SalesOrderLine::withoutGlobalScopes()
            ->where('tenant_id', $tenantId)
            ->where('sales_order_id', $salesOrderId)
            ->orderBy('id')
            ->lockForUpdate()
            ->get();
    }

    public function lockLineForTenant(int $tenantId, int $lineId): ?SalesOrderLine
    {
        return SalesOrderLine::withoutGlobalScopes()
            ->where('tenant_id', $tenantId)
            ->whereKey($lineId)
            ->lockForUpdate()
            ->first();
    }

    public function save(SalesOrderLine $line): void
    {
        $line->save();
    }
}
