<x-app-layout>
    <div class="portal-container narrow">
        <div class="page-head">
            <div>
                <span class="eyebrow">Domains</span>
                <h1 class="page-title">Domain Search</h1>
                <p class="page-subtitle">Search a domain, check availability, and add available register/transfer pricing to cart.</p>
            </div>
            <div class="currency-note">
                <strong>{{ $cart->currency }}</strong>
                <span>
                    @if (($currencyInfo['source'] ?? null) === 'browser_ip')
                        Currency selected automatically
                    @elseif (($currencyInfo['source'] ?? null) === 'ip')
                        Currency selected automatically
                    @else
                        {{ ucfirst($currencyInfo['source']) }}
                    @endif
                </span>
            </div>
        </div>

        @if ($errors->any())
            <div class="alert alert-error">{{ $errors->first() }}</div>
        @endif
        @if (session('status'))
            <div class="alert alert-success">{{ session('status') }}</div>
        @endif

        <div class="card domain-search-card">
            <form method="POST" action="{{ route('domains.search') }}" class="domain-search-form">
                @csrf
                <div class="domain-tabs">
                    <label><input type="radio" name="operation" value="register" {{ ($operation ?? 'register') === 'register' ? 'checked' : '' }}> Register</label>
                    <label><input type="radio" name="operation" value="transfer" {{ ($operation ?? '') === 'transfer' ? 'checked' : '' }}> Transfer</label>
                </div>
                <div class="domain-search-input-row">
                    <input class="input domain-input" name="domain_name" value="{{ old('domain_name', $domainResult['domain'] ?? '') }}" placeholder="example.com" required>
                    <button class="primary-btn" type="submit">Search</button>
                </div>
            </form>
        </div>

        @if ($domainResult)
            <div class="card result-card {{ $domainResult['availability']['status'] === 'available' ? 'available' : 'unavailable' }}">
                <div class="result-header">
                    <div>
                        <span class="eyebrow">{{ ucfirst($domainResult['operation']) }}</span>
                        <h2>{{ $domainResult['domain'] }}</h2>
                        <p>Availability source: {{ str_replace('_', ' ', $domainResult['availability']['source']) }}</p>
                    </div>
                    <span class="result-status">{{ ucfirst($domainResult['availability']['status']) }}</span>
                </div>

                @if ($domainResult['availability']['status'] === 'available' || $domainResult['operation'] === 'transfer')
                    @if (count($domainResult['prices']))
                        <form method="POST" action="{{ route('cart.add-domain') }}" class="price-options">
                            @csrf
                            <input type="hidden" name="domain_mode" value="{{ $domainResult['operation'] }}">
                            <input type="hidden" name="domain_name" value="{{ $domainResult['domain'] }}">
                            @if ($domainResult['operation'] === 'transfer')
                                <div class="form-group"><label>EPP/Auth Code</label><input class="input" name="epp_code" placeholder="Required for transfer"></div>
                            @endif
                            <div class="domain-price-grid">
                                @foreach ($domainResult['prices'] as $year => $price)
                                    <label class="domain-price-option">
                                        <input type="radio" name="years" value="{{ $year }}" {{ $loop->first ? 'checked' : '' }}>
                                        <span>{{ $year }} Year{{ $year > 1 ? 's' : '' }}</span>
                                        <strong>{{ $pricing->formatMoney($price['unit_price'], $cart->currency) }}</strong>
                                    </label>
                                @endforeach
                            </div>
                            <button class="primary-btn" type="submit">Add Domain to Cart</button>
                        </form>
                    @else
                        <div class="alert alert-error">Pricing is not available for the selected domain extension and operation.</div>
                    @endif
                @else
                    <div class="alert alert-error">This domain appears unavailable. Try another name or select transfer if you already own it.</div>
                @endif

                @if (count($domainResult['alternatives'] ?? []))
                    <div class="domain-alternative-list" style="margin-top:18px;">
                        <h3>Available alternatives</h3>
                        @foreach ($domainResult['alternatives'] as $alt)
                            <form method="POST" action="{{ route('cart.add-domain') }}" class="domain-alt-row">
                                @csrf
                                <input type="hidden" name="domain_mode" value="{{ $domainResult['operation'] }}">
                                <input type="hidden" name="domain_name" value="{{ $alt['domain'] }}">
                                <input type="hidden" name="years" value="{{ $alt['year'] }}">
                                <strong>{{ $alt['domain'] }}</strong>
                                <span>{{ $alt['year'] }} Year{{ $alt['year'] > 1 ? 's' : '' }} - {{ $pricing->formatMoney($alt['price']['unit_price'], $cart->currency) }} <small>(available now)</small></span>
                                <button class="secondary-btn" type="submit">Select</button>
                            </form>
                        @endforeach
                    </div>
                @endif
            </div>
        @endif
    </div>
    @include('components.browser-currency-detector', ['cart' => $cart, 'currencyInfo' => $currencyInfo])

</x-app-layout>
