<x-filament-panels::page>
    {{-- Info Banner --}}
    <x-filament::section
        icon="heroicon-o-information-circle"
        icon-color="info"
    >
        <x-slot name="heading">{{ __('Tip') }}</x-slot>
        <x-slot name="description">{{ __('Jabali Panel includes the Jabali Cache plugin for optimal caching performance. Enable it via the Cache toggle in your site\'s settings. After installing WordPress, complete the setup wizard, install security plugins, and keep everything updated.') }}</x-slot>
    </x-filament::section>

    {{ $this->table }}

    {{-- Security Scan Results Modal --}}
    <x-filament::modal
        id="security-scan-modal"
        wire:model="showSecurityScanModal"
        width="2xl"
        icon="heroicon-o-shield-check"
        :icon-color="count($securityScanResults['vulnerabilities'] ?? []) > 0 ? 'danger' : 'success'"
    >
        <x-slot name="heading">{{ __('Security Scan Results') }}</x-slot>
        <x-slot name="description">{{ $securityScanResults['url'] ?? '' }}</x-slot>

        {{-- Scan Info --}}
        <x-filament::section compact>
            <x-slot name="heading">{{ __('Scan Information') }}</x-slot>
            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 text-sm">
                <div>
                    <span class="text-gray-500 dark:text-gray-400">{{ __('Scanned URL') }}</span>
                    <div class="font-medium text-gray-950 dark:text-white">{{ $securityScanResults['url'] ?? __('Unknown') }}</div>
                </div>
                <div>
                    <span class="text-gray-500 dark:text-gray-400">{{ __('Scan Time') }}</span>
                    <div class="font-medium text-gray-950 dark:text-white">{{ $securityScanResults['scan_time'] ?? '' }}</div>
                </div>
            </div>
        </x-filament::section>

        @if(isset($securityScanResults['error']))
            <div class="p-4 rounded-lg bg-danger-50 dark:bg-danger-400/10 ring-1 ring-danger-600/10 dark:ring-danger-400/20">
                <div class="font-semibold text-danger-600 dark:text-danger-400 mb-2">{{ __('Scan Error') }}</div>
                <div class="text-sm text-danger-500">{{ $securityScanResults['error'] }}</div>
            </div>
        @else
            {{-- WordPress Version --}}
            @if(isset($securityScanResults['wordpress_version']))
                <x-filament::section compact>
                    <x-slot name="heading">{{ __('WordPress Version') }}</x-slot>
                    <div class="flex items-center gap-2">
                        <span class="text-base font-semibold text-gray-950 dark:text-white">{{ $securityScanResults['wordpress_version']['number'] }}</span>
                        @if(($securityScanResults['wordpress_version']['status'] ?? '') === 'insecure')
                            <x-filament::badge color="danger">{{ __('Insecure') }}</x-filament::badge>
                        @elseif(($securityScanResults['wordpress_version']['status'] ?? '') === 'outdated')
                            <x-filament::badge color="warning">{{ __('Outdated') }}</x-filament::badge>
                        @else
                            <x-filament::badge color="success">{{ __('Latest') }}</x-filament::badge>
                        @endif
                    </div>
                </x-filament::section>
            @endif

            {{-- Vulnerabilities --}}
            @if(count($securityScanResults['vulnerabilities'] ?? []) > 0)
                <x-filament::section
                    icon="heroicon-o-exclamation-triangle"
                    icon-color="danger"
                    compact
                >
                    <x-slot name="heading">{{ __('Vulnerabilities Found') }} ({{ count($securityScanResults['vulnerabilities']) }})</x-slot>
                    <div class="space-y-2">
                        @foreach($securityScanResults['vulnerabilities'] as $vuln)
                            <div class="p-3 rounded-lg bg-danger-50 dark:bg-danger-400/10 ring-1 ring-danger-600/10 dark:ring-danger-400/20">
                                <div class="text-xs text-danger-600 dark:text-danger-400 font-semibold mb-1">{{ $vuln['type'] }}</div>
                                <div class="text-sm text-gray-950 dark:text-white">{{ $vuln['title'] }}</div>
                                @if($vuln['fixed_in'])
                                    <div class="text-xs text-gray-500 dark:text-gray-400 mt-1">{{ __('Fixed in:') }} {{ $vuln['fixed_in'] }}</div>
                                @endif
                            </div>
                        @endforeach
                    </div>
                </x-filament::section>
            @else
                <div class="p-6 rounded-lg bg-success-50 dark:bg-success-400/10 ring-1 ring-success-600/10 dark:ring-success-400/20 text-center">
                    <x-filament::icon icon="heroicon-o-check-circle" class="h-10 w-10 text-success-500 mx-auto mb-3" />
                    <div class="text-sm font-semibold text-success-600 dark:text-success-400">{{ __('No Vulnerabilities Found') }}</div>
                    <div class="text-xs text-success-500 mt-1">{{ __('Your WordPress site appears to be secure') }}</div>
                </div>
            @endif

            {{-- Interesting Findings --}}
            @if(count($securityScanResults['interesting_findings'] ?? []) > 0)
                <x-filament::section
                    icon="heroicon-o-eye"
                    collapsible
                    collapsed
                    compact
                >
                    <x-slot name="heading">{{ __('Interesting Findings') }}</x-slot>
                    <div class="space-y-1">
                        @foreach(array_slice($securityScanResults['interesting_findings'], 0, 10) as $finding)
                            <div class="p-2 text-xs text-gray-600 dark:text-gray-400 rounded bg-gray-50 dark:bg-white/5">
                                {{ $finding['description'] }}
                            </div>
                        @endforeach
                    </div>
                </x-filament::section>
            @endif

            {{-- Detected Plugins --}}
            @if(count($securityScanResults['plugins'] ?? []) > 0)
                <x-filament::section
                    icon="heroicon-o-puzzle-piece"
                    collapsible
                    collapsed
                    compact
                >
                    <x-slot name="heading">{{ __('Detected Plugins') }} ({{ count($securityScanResults['plugins']) }})</x-slot>
                    <div class="flex flex-wrap gap-2">
                        @foreach($securityScanResults['plugins'] as $plugin)
                            <x-filament::badge color="gray">
                                {{ $plugin['name'] }} v{{ $plugin['version'] }}
                            </x-filament::badge>
                        @endforeach
                    </div>
                </x-filament::section>
            @endif
        @endif

        <x-slot name="footerActions">
            <x-filament::button wire:click="closeSecurityScanModal" color="gray">{{ __('Close') }}</x-filament::button>
        </x-slot>
    </x-filament::modal>

    <x-filament-actions::modals />

    @script
    <script>
        $wire.on('open-url', ({ url }) => {
            window.open(url, '_blank');
        });
    </script>
    @endscript
</x-filament-panels::page>
