@php
    $summaryStep = $checkoutStep ?? request('step', 'cart');
    if (! in_array($summaryStep, ['cart', 'account', 'payment'], true)) {
        $summaryStep = 'cart';
    }
    $subtotal = (float) $cart->items->sum('total');
    $discount = min(max((float) $cart->discount, 0), $subtotal);
    $totalDue = max(0, $subtotal - $discount);
@endphp

<h2>Order Summary</h2>
@forelse ($cart->items as $item)
    @php
        $isDomain = str_starts_with((string) $item->item_type, 'domain_');
        $domainDescription = match ($item->item_type) {
            'domain_register' => 'Domain registration'.($item->years ? ' - '.$item->years.' year'.((int) $item->years === 1 ? '' : 's') : ''),
            'domain_transfer' => 'Domain transfer'.($item->years ? ' - '.$item->years.' year'.((int) $item->years === 1 ? '' : 's') : ''),
            'domain_existing' => 'Existing domain',
            default => null,
        };
    @endphp
    <div class="summary-item">
        <div class="summary-item-info">
            <strong>{{ $isDomain && $item->domain_name ? $item->domain_name : $item->name }}</strong>
            @if ($isDomain)
                <span>{{ $domainDescription }}</span>
            @elseif ($item->billing_cycle)
                <span>{{ $pricing->cycleLabel($item->billing_cycle) }}</span>
            @endif
        </div>
        <b>{{ $pricing->formatMoney($item->total, $item->currency) }}</b>
    </div>
@empty
    <p class="empty-summary">No products selected yet.</p>
@endforelse

@if (! $cart->items->isEmpty())
    <div class="zt-promo-block">
        @if ($cart->promo_code)
            <div class="zt-promo-applied">
                <div><small>Promo code</small><strong>{{ $cart->promo_code }}</strong></div>
                <form method="POST" action="{{ route('cart.promotion.remove') }}" data-ajax-cart>
                    @csrf
                    @method('DELETE')
                    <button type="submit">Remove</button>
                </form>
            </div>
        @else
            <form method="POST" action="{{ route('cart.promotion.apply') }}" class="zt-promo-form" data-ajax-cart>
                @csrf
                <label for="promo-code">Have a promo code?</label>
                <div>
                    <input id="promo-code" class="input" name="promo_code" placeholder="Enter code" autocomplete="off">
                    <button type="submit">Apply</button>
                </div>
            </form>
        @endif
    </div>
@endif

<div class="summary-line"><span>Subtotal</span><strong>{{ $pricing->formatMoney($subtotal, $cart->currency) }}</strong></div>
@if ($discount > 0)
    <div class="summary-line zt-discount-line"><span>Discount</span><strong>-{{ $pricing->formatMoney($discount, $cart->currency) }}</strong></div>
@endif
<div class="summary-line"><span>Taxes</span><strong>{{ $pricing->formatMoney(0, $cart->currency) }}</strong></div>
<div class="summary-total"><span>Total</span><strong>{{ $pricing->formatMoney($totalDue, $cart->currency) }}</strong></div>

@if ($summaryStep === 'cart')
    @if ($cart->items->isEmpty())
        <button class="primary-btn complete-btn" type="button" disabled>Continue</button>
    @else
        <a class="primary-btn complete-btn" href="{{ route('checkout.index', ['step' => 'account']) }}#checkout-step">Continue</a>
    @endif
@elseif ($summaryStep === 'account')
    <button class="primary-btn complete-btn" form="checkout-account-form" type="submit" {{ $cart->items->isEmpty() ? 'disabled' : '' }}>Continue to Payment</button>
@else
    <button class="primary-btn complete-btn" form="complete-order-form" type="submit" {{ $cart->items->isEmpty() ? 'disabled' : '' }}>Place Order</button>
@endif
