<x-app-layout>
    <x-module-hero
        title="{{ $mode === 'create' ? 'New SMS Configuration' : 'Edit SMS Configuration' }}"
        description="{{ $mode === 'create' ? 'Set up your Africa\'s Talking API credentials' : 'Update your Africa\'s Talking API credentials' }}"
        icon="fa-mobile-screen">
        <x-slot name="backButton">
            <a href="{{ route('config.sms.index') }}"
               class="inline-flex items-center gap-1 text-white/80 hover:text-white text-sm transition-colors">
                <i class="fas fa-arrow-left"></i> Back to Dashboard
            </a>
        </x-slot>
        <x-slot name="tabs">
            <a href="{{ route('config.sms.index') }}"
               class="module-hero-tab text-[#1e293b]">
                <i class="fas fa-tachometer-alt"></i> Dashboard
            </a>
            <a href="{{ $mode === 'edit' ? route('config.sms.edit') : route('config.sms.create') }}"
               class="module-hero-tab active-tab text-[#1e293b]">
                <i class="fas fa-{{ $mode === 'edit' ? 'edit' : 'plus' }}"></i>
                {{ $mode === 'edit' ? 'Edit Configuration' : 'New Configuration' }}
            </a>
            <a href="{{ route('config.sms.logs') }}"
               class="module-hero-tab text-[#1e293b]">
                <i class="fas fa-history"></i> Logs
            </a>
            <a href="{{ route('config.sms.documentation') }}"
               class="module-hero-tab text-[#1e293b]">
                <i class="fas fa-book"></i> Documentation
            </a>
        </x-slot>
    </x-module-hero>

    <div class="p-6">
        @if(session('success'))
            <div class="mb-4 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-center">
                <i class="fas fa-check-circle mr-2"></i> {{ session('success') }}
            </div>
        @endif
        @if($errors->any())
            <div class="mb-4 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg">
                <div class="flex items-center mb-2">
                    <i class="fas fa-exclamation-circle mr-2"></i>
                    <span class="font-semibold">Please fix the following errors:</span>
                </div>
                <ul class="list-disc list-inside ml-4 text-sm">
                    @foreach($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <form action="{{ $mode === 'create' ? route('config.sms.store') : route('config.sms.update') }}"
              method="POST"
              x-data="{ showApiKey: false }">
            @csrf
            @if($mode === 'edit')
                @method('PUT')
            @endif

            <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
                {{-- API Credentials --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="bg-gradient-to-r from-green-600 to-green-700 px-6 py-4">
                        <h2 class="text-base font-semibold text-white flex items-center gap-2">
                            <i class="fas fa-key"></i> API Credentials
                        </h2>
                        <p class="text-green-100 text-xs mt-0.5">Africa's Talking API access details</p>
                    </div>
                    <div class="p-6 space-y-4">
                        {{-- API Key --}}
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">
                                API Key <span class="text-red-500">*</span>
                            </label>
                            <div class="relative">
                                <span class="absolute inset-y-0 left-0 pl-3 flex items-center text-gray-400"><i class="fas fa-key text-sm"></i></span>
                                <input :type="showApiKey ? 'text' : 'password'"
                                       name="api_key"
                                       value="{{ old('api_key', $configuration?->api_key ?? '') }}"
                                       placeholder="{{ $mode === 'edit' ? '•••••••• (leave blank to keep current)' : 'Enter your Africa\'s Talking API key' }}"
                                       class="w-full pl-9 pr-10 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-transparent text-sm"
                                       {{ $mode === 'create' ? 'required' : '' }}>
                                <button type="button" @click="showApiKey = !showApiKey"
                                        class="absolute inset-y-0 right-0 pr-3 flex items-center text-gray-400 hover:text-gray-600">
                                    <i :class="showApiKey ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
                                </button>
                            </div>
                            @if($mode === 'edit')
                                <p class="mt-1 text-xs text-gray-500"><i class="fas fa-info-circle mr-1"></i>Leave blank to keep the existing API key</p>
                            @endif
                            @error('api_key')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>

                        {{-- Username --}}
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">
                                Username <span class="text-red-500">*</span>
                            </label>
                            <div class="relative">
                                <span class="absolute inset-y-0 left-0 pl-3 flex items-center text-gray-400"><i class="fas fa-user text-sm"></i></span>
                                <input type="text" name="username"
                                       value="{{ old('username', $configuration?->username ?? '') }}"
                                       placeholder="Your Africa's Talking username"
                                       class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-transparent text-sm"
                                       required>
                            </div>
                            <p class="mt-1 text-xs text-gray-500">Use "sandbox" for testing or your app username for production</p>
                            @error('username')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>

                        {{-- Sender ID --}}
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">
                                Sender ID <span class="text-red-500">*</span>
                            </label>
                            <div class="relative">
                                <span class="absolute inset-y-0 left-0 pl-3 flex items-center text-gray-400"><i class="fas fa-id-badge text-sm"></i></span>
                                <input type="text" name="sender_id"
                                       value="{{ old('sender_id', $configuration?->sender_id ?? '') }}"
                                       placeholder="e.g., SSHIA"
                                       maxlength="11"
                                       class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-transparent text-sm uppercase"
                                       style="text-transform: uppercase;"
                                       required>
                            </div>
                            <p class="mt-1 text-xs text-gray-500">3–11 alphanumeric characters. Appears as sender name on recipients' phones.</p>
                            @error('sender_id')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>
                    </div>
                </div>

                {{-- Environment & Settings --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="bg-gradient-to-r from-blue-600 to-blue-700 px-6 py-4">
                        <h2 class="text-base font-semibold text-white flex items-center gap-2">
                            <i class="fas fa-server"></i> Environment Settings
                        </h2>
                        <p class="text-blue-100 text-xs mt-0.5">Select the API environment</p>
                    </div>
                    <div class="p-6 space-y-4">
                        {{-- Environment --}}
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-2">
                                Environment <span class="text-red-500">*</span>
                            </label>
                            <div class="grid grid-cols-2 gap-3">
                                <label class="relative flex items-center p-3 border-2 rounded-lg cursor-pointer transition
                                    {{ old('environment', $configuration?->environment ?? 'sandbox') === 'sandbox' ? 'border-yellow-500 bg-yellow-50' : 'border-gray-200 hover:border-yellow-300' }}">
                                    <input type="radio" name="environment" value="sandbox"
                                           {{ old('environment', $configuration?->environment ?? 'sandbox') === 'sandbox' ? 'checked' : '' }}
                                           class="sr-only">
                                    <div class="flex items-center gap-2">
                                        <i class="fas fa-flask text-yellow-500 text-lg"></i>
                                        <div>
                                            <span class="font-medium text-gray-800 text-sm">Sandbox</span>
                                            <p class="text-xs text-gray-500">For testing (free)</p>
                                        </div>
                                    </div>
                                </label>
                                <label class="relative flex items-center p-3 border-2 rounded-lg cursor-pointer transition
                                    {{ old('environment', $configuration?->environment ?? 'sandbox') === 'production' ? 'border-green-500 bg-green-50' : 'border-gray-200 hover:border-green-300' }}">
                                    <input type="radio" name="environment" value="production"
                                           {{ old('environment', $configuration?->environment ?? 'sandbox') === 'production' ? 'checked' : '' }}
                                           class="sr-only">
                                    <div class="flex items-center gap-2">
                                        <i class="fas fa-server text-green-500 text-lg"></i>
                                        <div>
                                            <span class="font-medium text-gray-800 text-sm">Production</span>
                                            <p class="text-xs text-gray-500">Live messages (paid)</p>
                                        </div>
                                    </div>
                                </label>
                            </div>
                            @error('environment')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>

                        {{-- Active Status --}}
                        <div class="flex items-center gap-3 p-3 bg-gray-50 rounded-lg">
                            <input type="checkbox" id="is_active" name="is_active" value="1"
                                   {{ old('is_active', $configuration?->is_active ?? true) ? 'checked' : '' }}
                                   class="w-4 h-4 text-green-600 rounded focus:ring-green-500 border-gray-300">
                            <label for="is_active" class="text-sm font-medium text-gray-700">
                                Set as active configuration
                            </label>
                        </div>

                        {{-- Security Notice --}}
                        <div class="bg-blue-50 border border-blue-200 rounded-lg p-3">
                            <div class="flex items-start gap-2">
                                <i class="fas fa-shield-alt text-blue-500 mt-0.5"></i>
                                <div>
                                    <h4 class="font-medium text-blue-800 text-sm">Security Notice</h4>
                                    <p class="text-xs text-blue-700 mt-0.5">
                                        Your API credentials are encrypted and stored securely. Never share your API key with anyone.
                                    </p>
                                </div>
                            </div>
                        </div>

                        {{-- Help --}}
                        <div class="bg-gray-50 rounded-lg p-3">
                            <h4 class="font-medium text-gray-800 text-sm mb-2">Getting Started</h4>
                            <ol class="text-xs text-gray-600 space-y-1 list-decimal list-inside">
                                <li>Create an account at <a href="https://africastalking.com" target="_blank" class="text-green-600 hover:underline">Africa's Talking</a></li>
                                <li>Create a new application in your dashboard</li>
                                <li>Copy your API key and username</li>
                                <li>Start with Sandbox mode for testing</li>
                            </ol>
                        </div>
                    </div>
                </div>
            </div>

            {{-- Form Actions --}}
            <div class="flex items-center justify-between">
                <a href="{{ route('config.sms.index') }}"
                   class="px-4 py-2 bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 transition text-sm">
                    <i class="fas fa-arrow-left mr-1"></i> Back to Dashboard
                </a>
                <button type="submit"
                        class="px-6 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition text-sm">
                    <i class="fas fa-save mr-1"></i>
                    {{ $mode === 'create' ? 'Save Configuration' : 'Update Configuration' }}
                </button>
            </div>
        </form>
    </div>
</x-app-layout>
