<section>
    <header>
        <h2 class="text-lg font-medium text-gray-900">
            {{ __('Verified Accounts') }}
        </h2>
        <p class="mt-1 text-sm text-gray-600">
            Link your external profiles to build trust with buyers and sellers.
        </p>
    </header>

    <div class="mt-6 space-y-6">
        @if($activeServices->isEmpty())
            <p class="text-gray-600">No trust services available yet.</p>
        @else
            @foreach($activeServices as $service)
                @php
                    $userLink = auth()->user()->trustLinks()
                        ->where('trust_service_id', $service->id)
                        ->first();
                @endphp

                <div class="border border-gray-200 rounded-lg p-4">
                    <div class="flex items-center justify-between mb-3">
                        <div>
                            <h3 class="font-semibold text-gray-900">{{ $service->name }}</h3>
                            @if($service->description)
                                <p class="text-sm text-gray-600">{{ $service->description }}</p>
                            @endif
                        </div>
                        @if($userLink)
                            <span class="px-3 py-1 rounded-full text-xs font-semibold
                                @if($userLink->status === 'verified')
                                    bg-green-100 text-green-700
                                @elseif($userLink->status === 'challenge_sent' || $userLink->status === 'submitted')
                                    bg-yellow-100 text-yellow-700
                                @else
                                    bg-gray-100 text-gray-700
                                @endif
                            ">
                                {{ ucfirst(str_replace('_', ' ', $userLink->status)) }}
                            </span>
                        @else
                            <span class="px-3 py-1 rounded-full text-xs font-semibold bg-gray-100 text-gray-700">
                                Not Connected
                            </span>
                        @endif
                    </div>

                    @if(!$userLink)
                        <form method="POST" action="{{ route('trust.store', $service) }}" class="space-y-3">
                            @csrf
                            <div>
                                <x-input-label for="remote_url_{{ $service->id }}" value="Your Profile URL" />
                                <x-text-input
                                    id="remote_url_{{ $service->id }}"
                                    name="remote_user_url"
                                    type="url"
                                    class="w-full"
                                    placeholder="https://{{ parse_url($service->url, PHP_URL_HOST) }}/your-username"
                                    required
                                />
                                @error('remote_user_url')<span class="text-red-600 text-sm">{{ $message }}</span>@enderror
                            </div>
                            <button type="submit" class="inline-flex items-center px-4 py-2 bg-gray-800 text-white rounded-lg hover:bg-gray-700 text-sm font-medium">
                                Connect {{ $service->name }}
                            </button>
                        </form>
                    @elseif($userLink->status === 'challenge_sent')
                        <div class="bg-blue-50 border border-blue-200 rounded-lg p-3 mb-3">
                            <p class="text-sm text-blue-700 mb-2"><strong>Challenge Code:</strong> <code class="bg-white px-2 py-1 rounded font-mono">{{ $userLink->challenge_code }}</code></p>
                            @if($service->verification_mode === 'automated')
                                <p class="text-sm text-blue-600 mb-3">Post this code publicly on {{ $service->name }}, then submit the URL of your post below.</p>
                                <form method="POST" action="{{ route('trust.proof', $userLink) }}" class="space-y-2">
                                    @csrf
                                    <div>
                                        <x-input-label for="proof_url_{{ $userLink->id }}" value="URL of Your Post" />
                                        <x-text-input
                                            id="proof_url_{{ $userLink->id }}"
                                            name="remote_proof_url"
                                            type="url"
                                            class="w-full"
                                            required
                                        />
                                    </div>
                                    <button type="submit" class="inline-flex items-center px-3 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 text-sm font-medium">
                                        Submit Proof
                                    </button>
                                </form>
                            @else
                                <p class="text-sm text-blue-600">Send this code via private message to a support account on {{ $service->name }}. An admin will verify within 48 hours.</p>
                            @endif
                        </div>
                        <button onclick="if(confirm('Disconnect from {{ $service->name }}?')) document.getElementById('disconnect_{{ $userLink->id }}').submit();" class="text-red-600 hover:text-red-700 text-sm font-medium">
                            Disconnect
                        </button>
                        <form id="disconnect_{{ $userLink->id }}" method="POST" action="{{ route('admin.trust-links.destroy', $userLink) }}" style="display:none;">
                            @csrf
                            @method('DELETE')
                        </form>
                    @elseif($userLink->status === 'submitted')
                        <div class="bg-yellow-50 border border-yellow-200 rounded-lg p-3">
                            <p class="text-sm text-yellow-700">Verification in progress. We'll notify you once verified.</p>
                        </div>
                    @elseif($userLink->status === 'verified')
                        <div class="bg-green-50 border border-green-200 rounded-lg p-3 flex items-center justify-between">
                            <span class="text-sm text-green-700">✓ Your {{ $service->name }} account is verified!</span>
                            <a href="{{ $userLink->remote_user_url }}" target="_blank" class="text-green-600 hover:text-green-700 text-sm font-medium">View Profile →</a>
                        </div>
                    @endif
                </div>
            @endforeach
        @endif
    </div>
</section>
