<x-app-layout>
    <x-slot name="header">
        <div class="flex items-center justify-between">
            <h2 class="font-semibold text-lg text-gray-800 dark:text-gray-200 leading-tight">
                Istoric modificări - {{ $recordName }}
            </h2>
            <a href="{{ route('dns-zones.show', $zoneName) }}" class="text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-200">
                <i class="fas fa-arrow-left mr-2"></i>
                Înapoi la zona DNS
            </a>
        </div>
    </x-slot>

    <div class="py-12">
        <div class="px-6 lg:px-8">
            <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6">
                    <div class="mb-6 p-4 bg-green-50 dark:bg-green-900/20 border border-green-200 dark:border-green-800 rounded-lg">
                        <div class="flex items-center">
                            <i class="fas fa-server text-green-600 dark:text-green-400 mr-2"></i>
                            <span class="text-green-800 dark:text-green-200 font-medium">Istoric DNS</span>
                        </div>
                        <p class="text-green-700 dark:text-green-300 text-sm mt-1">
                            @if(isset($specificContent) && $specificContent)
                                Modificările pentru înregistrarea <strong>{{ $recordName }}</strong> cu conținutul <strong class="font-mono bg-green-100 dark:bg-green-800 px-2 py-0.5 rounded">{{ $specificContent }}</strong> din zona <strong>{{ $zoneName }}</strong>
                            @else
                                Toate modificările pentru înregistrarea <strong>{{ $recordName }}</strong> din zona <strong>{{ $zoneName }}</strong>
                            @endif
                        </p>
                        @if(isset($specificContent) && $specificContent)
                            <div class="mt-2">
                                <a href="{{ route('powerdns.records.changelog', [$zoneName, $recordName]) }}" class="text-green-600 dark:text-green-400 hover:text-green-800 dark:hover:text-green-300 text-sm">
                                    <i class="fas fa-times-circle mr-1"></i>
                                    Vezi toate modificările (fără filtru)
                                </a>
                            </div>
                        @endif
                    </div>

                    @if($changelog->count() > 0)
                        <div class="overflow-x-auto">
                            <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
                                <thead class="bg-gray-50 dark:bg-gray-900">
                                    <tr>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">
                                            Data & Ora
                                        </th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">
                                            Utilizator
                                        </th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">
                                            Acțiune
                                        </th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">
                                            Detalii
                                        </th>
                                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">
                                            IP Address
                                        </th>
                                    </tr>
                                </thead>
                                <tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
                                    @foreach($changelog as $entry)
                                        <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/50 transition">
                                            <td class="px-6 py-4 whitespace-nowrap">
                                                <div class="text-sm text-gray-900 dark:text-gray-100">
                                                    {{ $entry->created_at->format('d.m.Y H:i:s') }}
                                                </div>
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap">
                                                <div class="text-sm font-medium text-gray-900 dark:text-gray-100">
                                                    {{ $entry->user->name }}
                                                </div>
                                                <div class="text-sm text-gray-500 dark:text-gray-400">
                                                    {{ $entry->user->email }}
                                                </div>
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap">
                                                @php
                                                    $actionColors = [
                                                        'create' => 'bg-green-100 dark:bg-green-900 text-green-800 dark:text-green-200',
                                                        'update' => 'bg-blue-100 dark:bg-blue-900 text-blue-800 dark:text-blue-200',
                                                        'delete' => 'bg-red-100 dark:bg-red-900 text-red-800 dark:text-red-200',
                                                        'activate' => 'bg-emerald-100 dark:bg-emerald-900 text-emerald-800 dark:text-emerald-200',
                                                        'deactivate' => 'bg-orange-100 dark:bg-orange-900 text-orange-800 dark:text-orange-200',
                                                        'restore' => 'bg-purple-100 dark:bg-purple-900 text-purple-800 dark:text-purple-200'
                                                    ];

                                                    $actionLabels = [
                                                        'create' => 'Creare',
                                                        'update' => 'Modificare',
                                                        'delete' => 'Ștergere',
                                                        'activate' => 'Activare',
                                                        'deactivate' => 'Dezactivare',
                                                        'restore' => 'Restaurare'
                                                    ];
                                                @endphp
                                                <span class="px-2 py-1 text-xs font-semibold rounded-full {{ $actionColors[$entry->action] ?? 'bg-gray-100 dark:bg-gray-700 text-gray-800 dark:text-gray-200' }}">
                                                    {{ $actionLabels[$entry->action] ?? ucfirst($entry->action) }}
                                                </span>
                                            </td>
                                            <td class="px-6 py-4">
                                                <div class="text-sm text-gray-900 dark:text-gray-100">
                                                    <strong>Tip:</strong> {{ $entry->record_type }}
                                                </div>

                                                @if($entry->old_content || $entry->new_content)
                                                    <div class="text-sm text-gray-600 dark:text-gray-400 mt-1">
                                                        @if($entry->action === 'delete' && $entry->old_content)
                                                            {{-- Pentru ștergere, afișează doar datele vechi --}}
                                                            <strong>Content șters:</strong><br>
                                                            <span class="text-red-600 dark:text-red-400">{{ $entry->old_content }}</span>
                                                        @elseif($entry->old_content && $entry->new_content && $entry->old_content !== $entry->new_content)
                                                            {{-- Pentru modificare, afișează diferența --}}
                                                            <strong>Content:</strong><br>
                                                            <span class="text-red-600 dark:text-red-400">- {{ $entry->old_content }}</span><br>
                                                            <span class="text-green-600 dark:text-green-400">+ {{ $entry->new_content }}</span>
                                                        @elseif($entry->new_content)
                                                            {{-- Pentru creare, afișează doar noul content --}}
                                                            <strong>Content:</strong> {{ $entry->new_content }}
                                                        @endif
                                                    </div>
                                                @endif

                                                @if($entry->old_ttl || $entry->new_ttl)
                                                    <div class="text-sm text-gray-600 dark:text-gray-400 mt-1">
                                                        @if($entry->action === 'delete' && $entry->old_ttl)
                                                            {{-- Pentru ștergere, afișează TTL-ul vechi --}}
                                                            <strong>TTL:</strong> <span class="text-red-600 dark:text-red-400">{{ $entry->old_ttl }}</span>
                                                        @elseif($entry->old_ttl && $entry->new_ttl && $entry->old_ttl !== $entry->new_ttl)
                                                            {{-- Pentru modificare, afișează schimbarea --}}
                                                            <strong>TTL:</strong> {{ $entry->old_ttl }} → {{ $entry->new_ttl }}
                                                        @elseif($entry->new_ttl)
                                                            {{-- Pentru creare, afișează noul TTL --}}
                                                            <strong>TTL:</strong> {{ $entry->new_ttl }}
                                                        @endif
                                                    </div>
                                                @endif

                                                @if($entry->action === 'activate' || $entry->action === 'deactivate')
                                                    <div class="text-sm text-gray-600 dark:text-gray-400 mt-1">
                                                        <strong>Status:</strong>
                                                        <span class="{{ $entry->new_disabled ? 'text-red-600 dark:text-red-400' : 'text-green-600 dark:text-green-400' }}">
                                                            {{ $entry->new_disabled ? 'Dezactivat' : 'Activ' }}
                                                        </span>
                                                    </div>
                                                @endif
                                            </td>
                                            <td class="px-6 py-4 whitespace-nowrap">
                                                <div class="text-sm text-gray-900 dark:text-gray-100">
                                                    @if($entry->ip_address === '::1')
                                                        <span class="text-blue-600 dark:text-blue-400" title="IPv6 Localhost">localhost (::1)</span>
                                                    @elseif($entry->ip_address === '127.0.0.1')
                                                        <span class="text-blue-600 dark:text-blue-400" title="IPv4 Localhost">localhost (127.0.0.1)</span>
                                                    @else
                                                        {{ $entry->ip_address }}
                                                    @endif
                                                </div>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>

                        <!-- Pagination -->
                        <div class="mt-6">
                            {{ $changelog->links() }}
                        </div>
                    @else
                        <div class="text-center py-12">
                            <i class="fas fa-history text-gray-400 dark:text-gray-600 text-6xl mb-4"></i>
                            <h3 class="text-xl font-semibold text-gray-900 dark:text-gray-100 mb-2">Fără istoric</h3>
                            <p class="text-gray-600 dark:text-gray-400 mb-6">Nu au fost găsite modificări pentru această înregistrare</p>
                            <a href="{{ route('dns-zones.show', $zoneName) }}" class="inline-flex items-center px-6 py-3 bg-indigo-600 hover:bg-indigo-700 text-white font-semibold rounded-lg transition">
                                <i class="fas fa-arrow-left mr-2"></i>
                                Înapoi la zona DNS
                            </a>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>

    <!-- Modal Confirmare Restore -->
    <div id="restoreModal" class="hidden fixed inset-0 bg-black bg-opacity-50 dark:bg-opacity-70 z-50 flex items-center justify-center p-4" onclick="if(event.target === this) closeRestoreModal()">
        <div class="relative w-full max-w-2xl bg-white dark:bg-gray-800 rounded-lg shadow-xl" onclick="event.stopPropagation()" style="max-width: min(42rem, 85vw);">
            <div class="p-6">
                <!-- Header -->
                <div class="flex items-center justify-between pb-4 border-b border-gray-200 dark:border-gray-700">
                    <h3 class="text-xl font-semibold text-gray-900 dark:text-gray-100">
                        <i class="fas fa-exclamation-triangle text-yellow-600 dark:text-yellow-400 mr-2"></i>
                        Confirmare Restaurare
                    </h3>
                    <button onclick="closeRestoreModal()" class="text-gray-400 hover:text-gray-600 dark:hover:text-gray-200 transition">
                        <i class="fas fa-times text-2xl"></i>
                    </button>
                </div>

                <!-- Content -->
                <div class="mt-4">
                    <div class="mb-4 p-4 bg-yellow-50 dark:bg-yellow-900/20 border border-yellow-200 dark:border-yellow-800 rounded-lg">
                        <p class="text-sm text-yellow-800 dark:text-yellow-200">
                            <i class="fas fa-info-circle mr-2"></i>
                            Sigur vrei să restaurezi această versiune? Modificările curente vor fi suprascrise.
                        </p>
                    </div>

                    <div class="space-y-3 text-sm">
                        <div class="flex items-start">
                            <span class="font-semibold text-gray-700 dark:text-gray-300 w-24">Data:</span>
                            <span class="text-gray-900 dark:text-gray-100" id="restoreDate"></span>
                        </div>
                        <div class="flex items-start">
                            <span class="font-semibold text-gray-700 dark:text-gray-300 w-24">Content:</span>
                            <span class="text-gray-900 dark:text-gray-100 break-all" id="restoreContent"></span>
                        </div>
                        <div class="flex items-start">
                            <span class="font-semibold text-gray-700 dark:text-gray-300 w-24">TTL:</span>
                            <span class="text-gray-900 dark:text-gray-100" id="restoreTtl"></span>
                        </div>
                    </div>
                </div>

                <!-- Actions -->
                <div class="flex items-center justify-end space-x-3 pt-6 border-t border-gray-200 dark:border-gray-700 mt-6">
                    <button onclick="closeRestoreModal()" class="px-4 py-2 bg-gray-200 dark:bg-gray-700 text-gray-700 dark:text-gray-300 font-semibold rounded-lg hover:bg-gray-300 dark:hover:bg-gray-600 transition">
                        Anulează
                    </button>
                    <button onclick="confirmRestore()" class="px-4 py-2 bg-green-600 hover:bg-green-700 text-white font-semibold rounded-lg transition">
                        <i class="fas fa-undo mr-2"></i>
                        Restaurează
                    </button>
                </div>
            </div>
        </div>
    </div>

    <script>
        let currentRestoreFormId = null;

        function showRestoreModal(entryId, date, content, ttl) {
            currentRestoreFormId = entryId;
            document.getElementById('restoreDate').textContent = date;
            document.getElementById('restoreContent').textContent = content;
            document.getElementById('restoreTtl').textContent = ttl;
            document.getElementById('restoreModal').classList.remove('hidden');
            document.body.style.overflow = 'hidden';
        }

        function closeRestoreModal() {
            document.getElementById('restoreModal').classList.add('hidden');
            document.body.style.overflow = 'auto';
            currentRestoreFormId = null;
        }

        function confirmRestore() {
            if (currentRestoreFormId) {
                document.getElementById('restore-form-' + currentRestoreFormId).submit();
            }
        }

        // Close modal on Escape key
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape') {
                closeRestoreModal();
            }
        });
    </script>
</x-app-layout>
