@extends('layouts.admin')

@section('title', 'Updates')

@section('page-title', 'System Updates')

@section('breadcrumbs')
    <a href="{{ route('admin.settings.index') }}" class="hover:text-primary">Settings</a>
    <span class="material-symbols-outlined text-xs">chevron_right</span>
    <span class="text-primary font-medium">Updates</span>
@endsection

@section('header-actions')
    <button id="check-updates-btn" class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-blue-600 transition-colors flex items-center gap-2">
        <span class="material-symbols-outlined text-lg">refresh</span>
        <span>Check for Updates</span>
    </button>
@endsection

@section('content')
<div class="max-w-7xl mx-auto">
    <!-- Current Version Card -->
    <div class="bg-white rounded-xl shadow-sm border border-[#e5e7eb] p-6 mb-6">
        <div class="flex items-center justify-between">
            <div>
                <h3 class="text-lg font-bold text-[#111418] text-gray-900 mb-2">Current Version</h3>
                <p class="text-2xl font-bold text-primary">{{ $currentVersion }}</p>
            </div>
            <div class="text-right">
                @if($hasAccess)
                    <span class="px-3 py-1 bg-green-100 text-green-700 rounded-lg text-sm font-medium">License Active</span>
                @else
                    <span class="px-3 py-1 bg-red-100 text-red-700 rounded-lg text-sm font-medium">License Required</span>
                @endif
            </div>
        </div>
    </div>

    @if(!$hasAccess)
        <!-- License Warning -->
        <div class="bg-yellow-50 border border-yellow-200 rounded-xl p-6 mb-6">
            <div class="flex items-start gap-3">
                <span class="material-symbols-outlined text-yellow-600">warning</span>
                <div>
                    <h4 class="font-bold text-yellow-900 mb-1">License Required</h4>
                    <p class="text-sm text-yellow-800">A valid license is required to check for and download updates. Please activate your license in <a href="{{ route('admin.license.index') }}" class="underline font-medium">License Settings</a>.</p>
                </div>
            </div>
        </div>
    @endif

    @if($latestUpdate)
        <!-- Latest Update Available -->
        <div class="bg-blue-50 border border-blue-200 rounded-xl p-6 mb-6">
            <div class="flex items-start justify-between">
                <div class="flex items-start gap-3">
                    <span class="material-symbols-outlined text-blue-600">system_update</span>
                    <div>
                        <h4 class="font-bold text-blue-900 mb-1">Update Available</h4>
                        <p class="text-sm text-blue-800 mb-2">Version <strong>{{ $latestUpdate->version }}</strong> is available for download.</p>
                        <a href="{{ route('admin.updates.show', $latestUpdate->id) }}" class="text-sm font-medium text-blue-700 hover:underline">View Update Details →</a>
                    </div>
                </div>
            </div>
        </div>
    @endif

    <!-- Available Updates -->
    <div class="bg-white rounded-xl shadow-sm border border-[#e5e7eb] mb-6">
        <div class="p-6 border-b border-[#e5e7eb]">
            <h3 class="text-lg font-bold text-[#111418] text-gray-900">Available Updates</h3>
        </div>
        <div class="p-6">
            @if(count($availableUpdates) > 0)
                <div class="space-y-4">
                    @foreach($availableUpdates as $update)
                        <div class="border border-[#e5e7eb] rounded-lg p-4 hover:bg-background-light transition-colors">
                            <div class="flex items-start justify-between">
                                <div class="flex-1">
                                    <div class="flex items-center gap-3 mb-2">
                                        <h4 class="text-lg font-bold text-[#111418] text-gray-900">{{ $update->title }}</h4>
                                        <span class="px-2 py-1 rounded-full text-xs font-medium {{ $update->getTypeColor() }}">
                                            {{ ucfirst($update->type) }}
                                        </span>
                                        <span class="px-2 py-1 bg-primary/10 text-primary rounded-full text-xs font-medium">
                                            v{{ $update->version }}
                                        </span>
                                    </div>
                                    @if($update->description)
                                        <p class="text-sm text-[#617589] mb-2">{{ $update->description }}</p>
                                    @endif
                                    @if($update->released_at)
                                        <p class="text-xs text-[#617589]">Released: {{ $update->released_at->format('M d, Y') }}</p>
                                    @endif
                                </div>
                                <div class="flex items-center gap-2 ml-4">
                                    <a href="{{ route('admin.updates.show', $update->id) }}" class="px-4 py-2 border border-[#dbe0e6] text-[#617589] rounded-lg hover:bg-background-light transition-colors text-sm">View Details</a>
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
            @else
                <div class="text-center py-12 text-[#617589]">
                    <span class="material-symbols-outlined text-4xl mb-4 block">check_circle</span>
                    <p class="text-lg font-medium mb-2">You're up to date!</p>
                    <p class="text-sm">No updates available at this time.</p>
                </div>
            @endif
        </div>
    </div>

    <!-- Installed Updates -->
    @if($installedUpdates->count() > 0)
        <div class="bg-white rounded-xl shadow-sm border border-[#e5e7eb]">
            <div class="p-6 border-b border-[#e5e7eb]">
                <h3 class="text-lg font-bold text-[#111418] text-gray-900">Recently Installed Updates</h3>
            </div>
            <div class="p-6">
                <div class="space-y-3">
                    @foreach($installedUpdates as $update)
                        <div class="flex items-center justify-between py-2 border-b border-[#e5e7eb] last:border-0">
                            <div>
                                <p class="text-sm font-medium text-[#111418] text-gray-900">{{ $update->title }}</p>
                                <p class="text-xs text-[#617589]">Version {{ $update->version }} • Installed {{ $update->installed_at->format('M d, Y') }}</p>
                            </div>
                            <span class="px-2 py-1 bg-green-100 text-green-700 rounded-full text-xs font-medium">Installed</span>
                        </div>
                    @endforeach
                </div>
            </div>
        </div>
    @endif

    <!-- Manual Update Guide -->
    <div class="bg-white rounded-xl shadow-sm border border-[#e5e7eb] mt-6">
        <div class="p-6 border-b border-[#e5e7eb]">
            <h3 class="text-lg font-bold text-[#111418] text-gray-900">Manual Update Guide</h3>
        </div>
        <div class="p-6">
            <div class="prose dark:prose-invert max-w-none">
                <ol class="list-decimal list-inside space-y-3 text-sm text-[#617589]">
                    <li><strong class="text-[#111418] text-gray-900">Backup your database and files</strong> - Always create a backup before updating</li>
                    <li><strong class="text-[#111418] text-gray-900">Download the update package</strong> - Download the update file from the update details page</li>
                    <li><strong class="text-[#111418] text-gray-900">Extract the update files</strong> - Extract the downloaded ZIP file</li>
                    <li><strong class="text-[#111418] text-gray-900">Upload files via FTP</strong> - Upload the extracted files to your server, overwriting existing files</li>
                    <li><strong class="text-[#111418] text-gray-900">Run database migrations</strong> - Run <code class="bg-background-light px-2 py-1 rounded">php artisan migrate</code> in your terminal</li>
                    <li><strong class="text-[#111418] text-gray-900">Clear cache</strong> - Run <code class="bg-background-light px-2 py-1 rounded">php artisan cache:clear</code> and <code class="bg-background-light px-2 py-1 rounded">php artisan config:clear</code></li>
                    <li><strong class="text-[#111418] text-gray-900">Mark as installed</strong> - Click "Mark as Installed" on the update page after successful installation</li>
                </ol>
                <div class="mt-6 p-4 bg-yellow-50 border border-yellow-200 rounded-lg">
                    <p class="text-sm text-yellow-800"><strong>Important:</strong> Never use shell commands or automated update scripts. Always perform updates manually to ensure system stability and security.</p>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Loading Modal -->
<div id="checking-modal" data-admin-modal class="hidden fixed inset-0 bg-black/50 z-50 flex items-center justify-center">
    <div class="bg-white rounded-xl shadow-lg p-6 max-w-md w-full mx-4">
        <div class="flex items-center gap-3 mb-4">
            <div class="animate-spin rounded-full h-6 w-6 border-b-2 border-primary"></div>
            <h3 class="text-lg font-bold text-[#111418] text-gray-900">Checking for Updates...</h3>
        </div>
        <p class="text-sm text-[#617589]">Please wait while we check for available updates.</p>
    </div>
</div>

<script>
document.getElementById('check-updates-btn')?.addEventListener('click', async function() {
    const modal = document.getElementById('checking-modal');
    modal.classList.remove('hidden');
    
    try {
        const response = await fetch('{{ route("admin.updates.check") }}?force=true', {
            method: 'GET',
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content
            }
        });
        
        const data = await response.json();
        modal.classList.add('hidden');
        
        if (data.success) {
            if (data.has_update) {
                alert('Update available! Version ' + data.latest_version + ' is ready for download.');
                location.reload();
            } else {
                alert('You are already running the latest version.');
            }
        } else {
            alert('Update check failed: ' + (data.message || 'Unknown error'));
        }
    } catch (error) {
        modal.classList.add('hidden');
        alert('Error checking for updates: ' + error.message);
    }
});
</script>
@endsection



