<?php

namespace App\Modules\Sales\Services;

use App\Modules\Sales\DTOs\SalesFulfillLineResult;
use Illuminate\Support\Facades\DB;

final class SalesFulfillmentService
{
    public function __construct(
        private readonly SalesFulfillmentExecuteHandler $handler,
    ) {}

    public function fulfillLineQuantity(
        int $tenantId,
        int $salesOrderId,
        int $lineId,
        string $quantity,
        string $idempotencyKey,
        ?string $correlationId,
        ?int $actorUserId,
    ): SalesFulfillLineResult {
        $attempts = (int) config('inventory.transaction_attempts', 5);

        return DB::transaction(function () use (
            $tenantId,
            $salesOrderId,
            $lineId,
            $quantity,
            $idempotencyKey,
            $correlationId,
            $actorUserId,
        ): SalesFulfillLineResult {
            return $this->handler->execute(
                $tenantId,
                $salesOrderId,
                $lineId,
                $quantity,
                $idempotencyKey,
                $correlationId,
                $actorUserId,
            );
        }, $attempts);
    }
}
