<?php

namespace App\Modules\Sales\Services;

use App\Modules\Inventory\Stock\Exceptions\InsufficientStockException;
use App\Modules\Sales\Exceptions\SalesOperationalException;

final class SalesOperationalWebExceptionMapper
{
    public static function toOperationalMessage(\Throwable $e): string
    {
        if ($e instanceof InsufficientStockException) {
            $detail = $e->getMessage();

            return $detail !== ''
                ? __('sales.errors.insufficient_stock_detail', ['detail' => $detail])
                : __('sales.errors.insufficient_stock');
        }
        if ($e instanceof SalesOperationalException) {
            return match ($e->violationCode) {
                'sales_over_allocation' => __('sales.errors.over_allocation'),
                'sales_fulfill_no_reservation' => __('sales.errors.fulfill_no_reservation'),
                'sales_fulfill_qty_exceeds_reserved' => __('sales.errors.fulfill_exceeds_reserved'),
                'sales_reservation_drift' => __('sales.errors.reservation_drift'),
                'sales_allocate_state_invalid' => __('sales.errors.allocate_state_invalid'),
                'sales_fulfill_state_invalid' => __('sales.errors.fulfill_state_invalid'),
                'sales_cancel_invalid' => __('sales.errors.cancel_invalid'),
                default => $e->getMessage(),
            };
        }

        return __('sales.errors.generic');
    }
}
