@if (!empty($domainResult))
    @if (!empty($domainResult['error']))
        <div class="alert alert-error" style="margin-top:16px;">{{ $domainResult['error'] }}</div>
    @else
        @php
            $domainStatus = $domainResult['availability']['status'] ?? 'unknown';
            $domainOperation = $domainResult['operation'] ?? request('operation', 'register');

            $isRegister = $domainOperation === 'register';
            $isTransfer = $domainOperation === 'transfer';
            $isAvailable = $domainStatus === 'available';
            $isUnavailable = $domainStatus === 'unavailable';

            $canRegister = $isRegister && $isAvailable;
            $canTransfer = $isTransfer && $isUnavailable;
            $canAddDomain = $canRegister || $canTransfer;

            $statusLabel = match (true) {
                $canRegister => 'Available',
                $canTransfer => 'Transferable',
                $isRegister && $isUnavailable => 'Already Registered',
                $isTransfer && $isAvailable => 'Not Registered',
                default => 'Needs Manual Check',
            };

            $statusMessage = match (true) {
                $canRegister => 'This domain is available. Select a billing period and add it to cart.',
                $canTransfer => 'This domain appears registered. Enter the EPP/Auth code to transfer it.',
                $isRegister && $isUnavailable => 'This domain already exists. Choose another suggested domain below.',
                $isTransfer && $isAvailable => 'This domain is not registered yet. Please choose Register instead of Transfer.',
                default => 'The availability check could not be completed. Please try again.',
            };
        @endphp

        <div class="inline-domain-result {{ $canAddDomain ? 'is-available' : 'is-unavailable' }}">
            <div class="domain-result-main">
                <div>
                    <span class="domain-result-status">{{ $statusLabel }}</span>
                    <h3>{{ $domainResult['domain'] }}</h3>
                    <p>{{ $statusMessage }}</p>
                </div>
            </div>

            @if ($canAddDomain && count($domainResult['prices'] ?? []))
                <form method="POST" action="{{ route('cart.add-domain') }}" class="domain-dropdown-actions" data-ajax-cart>
                    @csrf

                    <input type="hidden" name="domain_mode" value="{{ $domainOperation }}">
                    <input type="hidden" name="domain_name" value="{{ $domainResult['domain'] }}">
                    <input type="hidden" name="domain_checked" value="1">
                    <input type="hidden" name="domain_status" value="{{ $domainStatus }}">

                    @if ($isTransfer)
                        <div class="domain-dropdown-card">
                            <label for="domain-epp-code">EPP/Auth Code</label>
                            <input id="domain-epp-code" class="input" name="epp_code" placeholder="Enter transfer authorization code" required>
                        </div>
                    @endif

                    <div class="domain-dropdown-card">
                        <label for="domain-years-select">Choose Billing Period</label>
                        <select id="domain-years-select" class="select domain-years-select" name="years" required>
                            @foreach ($domainResult['prices'] as $year => $priceRow)
                                <option value="{{ $year }}">
                                    {{ $year }} Year{{ $year > 1 ? 's' : '' }} — {{ $pricing->formatMoney($priceRow['unit_price'], $cart->currency) }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <button class="primary-btn domain-add-btn" type="submit">
                        {{ $isTransfer ? 'Add Transfer to Cart' : 'Add Domain to Cart' }}
                    </button>
                </form>
            @elseif ($canAddDomain)
                <div class="alert alert-error" style="margin-top:16px;">Pricing is not available for this domain extension and operation.</div>
            @endif

            @if ($isRegister && count($domainResult['suggestion_candidates'] ?? []))
                <div class="domain-alternative-list" id="zt-domain-alternative-list" data-operation="register" data-max-results="{{ (int) config('zt.domain_suggestions.max_results', 6) }}" data-candidates='@json(array_values($domainResult['suggestion_candidates'] ?? []))'>
                    <h3 style="margin:0 0 8px;">More domain suggestions</h3>
                    <p data-domain-suggestion-status style="margin:0 0 12px;color:#6b7280;font-size:14px;">Checking more suggestions one by one...</p>
                    <div data-domain-suggestion-items>
                        @foreach (($domainResult['alternatives'] ?? []) as $alt)
                            @include('checkout.partials.domain-suggestion-row', ['alt' => $alt, 'cart' => $cart, 'pricing' => $pricing])
                        @endforeach
                    </div>
                </div>
            @endif
        </div>
    @endif
@endif
