<?php

namespace App\Domains\Purchase\Services\Checkout;

use App\Domains\Catalog\Enums\InventorySubject;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Variant;

class InsufficientStockMessageBuilder
{
    public function build(
        CatalogItem $catalogItem,
        CatalogItem|Variant $selection,
        int $availableQuantity,
    ): string {
        return match ($catalogItem->inventory_subject) {
            InventorySubject::Seat => __('api.purchase.stock.seat_unavailable', [
                'selection' => $selection->getSelectionLabel(),
            ]),
            InventorySubject::Ticket => __('api.purchase.stock.ticket_unavailable', [
                'selection' => $selection->getSelectionLabel(),
            ]),
            InventorySubject::Product => __('api.purchase.stock.product_unavailable', [
                'selection' => $this->productSelectionLabel($catalogItem, $selection),
                'max' => $availableQuantity,
            ]),
        };
    }

    private function productSelectionLabel(
        CatalogItem $catalogItem,
        CatalogItem|Variant $selection,
    ): string {
        if ($selection instanceof CatalogItem) {
            return $selection->getSelectionLabel();
        }

        return __('api.purchase.stock.product_selection', [
            'product' => $catalogItem->getName(),
            'selection' => $selection->getSelectionLabel(),
        ]);
    }
}
