<?php

namespace App\Modules\Inventory\Reservation\Services;

use Illuminate\Support\Facades\Log;

final class InventoryReservationObservability
{
    private function enabled(): bool
    {
        return (bool) config('inventory.observability.enabled', true);
    }

    /** @param  array<string, mixed>  $context */
    public function allocationFailure(string $reason, array $context = []): void
    {
        if (! $this->enabled()) {
            return;
        }
        Log::warning('inventory.reservation.allocation_failure '.$reason, $context);
    }

    /** @param  array<string, mixed>  $context */
    public function reservationConflict(string $reason, array $context = []): void
    {
        if (! $this->enabled()) {
            return;
        }
        Log::notice('inventory.reservation.conflict '.$reason, $context);
    }

    /** @param  array<string, mixed>  $context */
    public function expirationRelease(array $context = []): void
    {
        if (! $this->enabled()) {
            return;
        }
        Log::info('inventory.reservation.expiration_release', $context);
    }

    /** @param  array<string, mixed>  $context */
    public function staleReservation(array $context = []): void
    {
        if (! $this->enabled()) {
            return;
        }
        Log::notice('inventory.reservation.stale', $context);
    }

    /** @param  array<string, mixed>  $context */
    public function reconciliationMismatch(string $message, array $context = []): void
    {
        if (! $this->enabled()) {
            return;
        }
        Log::warning('inventory.reservation.reconciliation_mismatch '.$message, $context);
    }

    /** @param  array<string, mixed>  $context */
    public function duplicateReplay(string $idempotencyKey, array $context = []): void
    {
        if (! $this->enabled()) {
            return;
        }
        Log::info('inventory.reservation.duplicate_replay', array_merge(['key' => $idempotencyKey], $context));
    }

    /** @param  array<string, mixed>  $context */
    public function deadlockRetry(int $attempt, array $context = []): void
    {
        if (! $this->enabled()) {
            return;
        }
        Log::notice('inventory.reservation.deadlock_retry', array_merge(['attempt' => $attempt], $context));
    }
}
