<x-app-layout>
    <x-module-hero
        title="{{ isset($configuration) ? 'Edit Email Configuration' : 'New Email Configuration' }}"
        description="{{ isset($configuration) ? 'Update your SMTP/IMAP settings' : 'Set up SMTP/IMAP credentials for email functionality' }}"
        icon="fa-envelope">
        <x-slot name="backButton">
            <a href="{{ route('config.email.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">
            @can('view email configuration')
            <a href="{{ route('config.email.index') }}"
               class="module-hero-tab {{ request()->routeIs('config.email.index') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium transition">
                <i class="fas fa-tachometer-alt"></i> Dashboard
            </a>
            @endcan
            @can('create email configuration')
            <a href="{{ isset($configuration) ? route('config.email.edit') : route('config.email.create') }}"
               class="module-hero-tab {{ request()->routeIs('config.email.create') || request()->routeIs('config.email.edit') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium transition">
                <i class="fas fa-{{ isset($configuration) ? 'edit' : 'plus' }}"></i>
                {{ isset($configuration) ? 'Edit Configuration' : 'New Configuration' }}
            </a>
            @endcan
            @can('view email logs')
            <a href="{{ route('config.email.logs') }}"
               class="module-hero-tab {{ request()->routeIs('config.email.logs') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium transition">
                <i class="fas fa-history"></i> Logs
            </a>
            @endcan
            <a href="{{ route('config.email.documentation') }}"
               class="module-hero-tab {{ request()->routeIs('config.email.documentation') ? 'border-b-2 border-[#1e293b]' : 'opacity-70 hover:opacity-100 transition-opacity' }} inline-flex items-center gap-2 px-4 py-3 text-[#1e293b] text-sm font-medium transition">
                <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

        <form method="POST"
              action="{{ isset($configuration) ? route('config.email.update') : route('config.email.store') }}">
            @csrf
            @if(isset($configuration))
                @method('PUT')
            @endif

            <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
                {{-- SMTP Configuration --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="bg-gradient-to-r from-blue-500 to-blue-600 px-6 py-4">
                        <h2 class="text-base font-semibold text-white flex items-center gap-2">
                            <i class="fas fa-paper-plane"></i> SMTP Configuration
                        </h2>
                        <p class="text-blue-100 text-xs mt-0.5">Configure outgoing email settings</p>
                    </div>
                    <div class="p-6 space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">
                                SMTP Host <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-server text-sm"></i></span>
                                <input type="text" name="smtp_host"
                                       value="{{ old('smtp_host', $configuration->smtp_host ?? $envConfig['smtp']['host'] ?? '') }}"
                                       class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm"
                                       placeholder="smtp.gmail.com" required>
                            </div>
                            @error('smtp_host')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">
                                SMTP Port <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-network-wired text-sm"></i></span>
                                <input type="number" name="smtp_port"
                                       value="{{ old('smtp_port', $configuration->smtp_port ?? $envConfig['smtp']['port'] ?? 587) }}"
                                       class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm"
                                       placeholder="587" min="1" max="65535" required>
                            </div>
                            <p class="mt-1 text-xs text-gray-500">Common: 25, 465 (SSL), 587 (TLS), 2525</p>
                            @error('smtp_port')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">
                                Encryption <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-lock text-sm"></i></span>
                                <select name="smtp_encryption"
                                        class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm">
                                    @foreach($encryptionTypes as $value => $label)
                                        <option value="{{ $value }}"
                                                {{ old('smtp_encryption', $configuration->smtp_encryption ?? $envConfig['smtp']['encryption'] ?? 'tls') === $value ? 'selected' : '' }}>
                                            {{ $label }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>
                            @error('smtp_encryption')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Username</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="smtp_username"
                                       value="{{ old('smtp_username', $configuration->smtp_username ?? $envConfig['smtp']['username'] ?? '') }}"
                                       class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm"
                                       placeholder="your-email@gmail.com">
                            </div>
                            @error('smtp_username')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Password</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="password" name="smtp_password"
                                       class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent text-sm"
                                       placeholder="{{ isset($configuration) && $configuration->smtp_username ? '•••••••• (leave blank to keep current)' : 'Enter password' }}"
                                       autocomplete="new-password">
                            </div>
                            @if(isset($configuration) && $configuration->smtp_username)
                                <p class="mt-1 text-xs text-gray-500"><i class="fas fa-info-circle mr-1"></i>Leave blank to keep the existing password</p>
                            @endif
                            @error('smtp_password')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>
                    </div>
                </div>

                {{-- IMAP Configuration --}}
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="bg-gradient-to-r from-purple-500 to-purple-600 px-6 py-4">
                        <h2 class="text-base font-semibold text-white flex items-center gap-2">
                            <i class="fas fa-inbox"></i> IMAP Configuration
                        </h2>
                        <p class="text-purple-100 text-xs mt-0.5">Configure incoming email settings (optional)</p>
                    </div>
                    <div class="p-6 space-y-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">IMAP Host</label>
                            <div class="relative">
                                <span class="absolute inset-y-0 left-0 pl-3 flex items-center text-gray-400"><i class="fas fa-server text-sm"></i></span>
                                <input type="text" name="imap_host"
                                       value="{{ old('imap_host', $configuration->imap_host ?? $envConfig['imap']['host'] ?? '') }}"
                                       class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent text-sm"
                                       placeholder="imap.gmail.com">
                            </div>
                            @error('imap_host')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">IMAP Port</label>
                            <div class="relative">
                                <span class="absolute inset-y-0 left-0 pl-3 flex items-center text-gray-400"><i class="fas fa-network-wired text-sm"></i></span>
                                <input type="number" name="imap_port"
                                       value="{{ old('imap_port', $configuration->imap_port ?? $envConfig['imap']['port'] ?? 993) }}"
                                       class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent text-sm"
                                       placeholder="993" min="1" max="65535">
                            </div>
                            <p class="mt-1 text-xs text-gray-500">Common: 143 (Standard), 993 (SSL)</p>
                            @error('imap_port')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Encryption</label>
                            <div class="relative">
                                <span class="absolute inset-y-0 left-0 pl-3 flex items-center text-gray-400"><i class="fas fa-lock text-sm"></i></span>
                                <select name="imap_encryption"
                                        class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent text-sm">
                                    @foreach($encryptionTypes as $value => $label)
                                        <option value="{{ $value }}"
                                                {{ old('imap_encryption', $configuration->imap_encryption ?? $envConfig['imap']['encryption'] ?? 'ssl') === $value ? 'selected' : '' }}>
                                            {{ $label }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>
                            @error('imap_encryption')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Username</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="imap_username"
                                       value="{{ old('imap_username', $configuration->imap_username ?? $envConfig['imap']['username'] ?? '') }}"
                                       class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent text-sm"
                                       placeholder="your-email@gmail.com">
                            </div>
                            @error('imap_username')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>

                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Password</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="password" name="imap_password"
                                       class="w-full pl-9 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-purple-500 focus:border-transparent text-sm"
                                       placeholder="{{ isset($configuration) && $configuration->imap_username ? '•••••••• (leave blank to keep current)' : 'Enter password' }}"
                                       autocomplete="new-password">
                            </div>
                            @if(isset($configuration) && $configuration->imap_username)
                                <p class="mt-1 text-xs text-gray-500"><i class="fas fa-info-circle mr-1"></i>Leave blank to keep the existing password</p>
                            @endif
                            @error('imap_password')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>
                    </div>
                </div>
            </div>

            {{-- Sender Information --}}
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden mb-6">
                <div class="bg-gradient-to-r from-green-500 to-green-600 px-6 py-4">
                    <h2 class="text-base font-semibold text-white flex items-center gap-2">
                        <i class="fas fa-user"></i> Sender Information
                    </h2>
                    <p class="text-green-100 text-xs mt-0.5">Default sender details for outgoing emails</p>
                </div>
                <div class="p-6">
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">From Address</label>
                            <div class="relative">
                                <span class="absolute inset-y-0 left-0 pl-3 flex items-center text-gray-400"><i class="fas fa-envelope text-sm"></i></span>
                                <input type="email" name="from_address"
                                       value="{{ old('from_address', $configuration->from_address ?? $envConfig['from']['address'] ?? '') }}"
                                       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"
                                       placeholder="noreply@example.com">
                            </div>
                            <p class="mt-1 text-xs text-gray-500">The email address that appears as the sender</p>
                            @error('from_address')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">From Name</label>
                            <div class="relative">
                                <span class="absolute inset-y-0 left-0 pl-3 flex items-center text-gray-400"><i class="fas fa-signature text-sm"></i></span>
                                <input type="text" name="from_name"
                                       value="{{ old('from_name', $configuration->from_name ?? $envConfig['from']['name'] ?? config('app.name')) }}"
                                       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"
                                       placeholder="{{ config('app.name') }}">
                            </div>
                            <p class="mt-1 text-xs text-gray-500">The name that appears as the sender</p>
                            @error('from_name')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                        </div>
                    </div>
                </div>
            </div>

            {{-- Form Actions --}}
            <div class="flex items-center justify-between">
                <a href="{{ route('config.email.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>
                <div class="flex gap-2">
                    <button type="button" onclick="testBeforeSave()"
                            class="px-4 py-2 bg-blue-50 text-blue-700 rounded-lg hover:bg-blue-100 transition text-sm">
                        <i class="fas fa-plug mr-1"></i> Test Connection
                    </button>
                    <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>
                        {{ isset($configuration) ? 'Update Configuration' : 'Save Configuration' }}
                    </button>
                </div>
            </div>
        </form>
    </div>

    @push('scripts')
    <script>
        function testBeforeSave() {
            fetch('{{ route("config.email.test") }}', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content },
                body: JSON.stringify({ type: 'smtp' })
            }).then(r => r.json()).then(d => {
                if (d.success) { alert('SMTP connection successful!\n\nYou can now save the configuration.'); }
                else { alert('SMTP connection failed:\n' + (d.error || d.message)); }
            }).catch(e => alert('Test failed: ' + e.message));
        }
    </script>
    @endpush
</x-app-layout>
