<div>
    {{-- Cabecera con breadcrumb --}}
    <div class="w-full flex items-center mb-8">
        <a href="{{ route('products.show', $product) }}"
            class="flex rounded-lg bg-white border border-gray-200 px-4 py-2 text-gray-700 hover:bg-gray-100 font-bold">
            <img src="{{ asset('vendor/blade-untitledui-icons/arrow-narrow-left.svg') }}" alt="Volver" class="w-6 h-6">
            <span class="ml-2">Volver</span>
        </a>
        <div class="ml-4 flex items-center text-sm text-gray-500">
            <a href="{{ route('products.scss-cdn.index', $product) }}"
               class="text-orange-600 hover:text-orange-800">
                Bundles SCSS CDN
            </a>
            <span class="mx-2">/</span>
            <span class="text-gray-700 font-semibold">Bundle {{ $bundle->version }}</span>
        </div>
    </div>

    {{-- Mensajes de éxito/error --}}
    @if (session()->has('success'))
        <div class="mb-4 rounded bg-green-100 px-4 py-2 text-green-700">
            {{ session('success') }}
        </div>
    @endif

    @if (session()->has('error'))
        <div class="mb-4 rounded bg-red-100 px-4 py-2 text-red-700">
            {{ session('error') }}
        </div>
    @endif

    {{-- Información del bundle --}}
    <div class="w-full flex flex-wrap items-center justify-between rounded-lg bg-white border border-gray-200 px-8 py-4 mb-6">
        <div class="w-full flex items-center justify-between">
            <div class="flex items-center gap-4">
                <div>
                    <h1 class="text-4xl font-bold">Bundle {{ $bundle->version }}</h1>
                    @if($bundle->description)
                        <p class="mt-2 text-gray-600">{{ $bundle->description }}</p>
                    @endif
                    <div class="mt-2 text-sm text-gray-500">
                        <span>Creado por: <span class="font-semibold text-gray-700">{{ $bundle->creator?->name ?? 'N/A' }}</span></span>
                        <span class="mx-2">•</span>
                        <span>Fecha: <span class="font-semibold text-gray-700">{{ $bundle->created_at->format('Y-m-d H:i') }}</span></span>
                    </div>
                </div>
            </div>
            <div class="flex items-center gap-2">
                <button wire:click="duplicateBundle"
                        wire:confirm="¿Seguro que deseas duplicar este bundle? Se creará un nuevo bundle con todos los archivos."
                        class="flex items-center rounded-lg bg-green-600 px-4 py-2 text-white hover:bg-green-700 font-semibold">
                    <img src="{{ asset('vendor/blade-untitledui-icons/copy.svg') }}" alt="Duplicar" class="w-5 h-5 mr-2 svg-fill-white">
                    Duplicar bundle
                </button>
                <a href="{{ route('products.scss-cdn.bundle.edit', [$product, $bundle]) }}"
                   class="flex items-center rounded-lg bg-orange-600 px-4 py-2 text-white hover:bg-orange-700 font-semibold">
                    <img src="{{ asset('vendor/blade-untitledui-icons/pencil.svg') }}" alt="Editar" class="w-5 h-5 mr-2 svg-fill-white">
                    Editar bundle
                </a>
            </div>
        </div>
    </div>

    {{-- Acciones y tabla de archivos --}}
    <div class="mb-6 flex items-center justify-between">
        <h2 class="text-2xl font-bold">Archivos ({{ $files->count() }})</h2>
        <div class="flex items-center gap-2">
            @if(!empty($selectedFiles))
                <button wire:click="deleteSelectedFiles"
                        wire:confirm="¿Seguro que deseas eliminar los {{ count($selectedFiles) }} archivo(s) seleccionado(s)?"
                        class="flex items-center rounded-lg bg-red-600 px-4 py-2 text-white hover:bg-red-700 font-semibold">
                    <img src="{{ asset('vendor/blade-untitledui-icons/trash.svg') }}" alt="Eliminar" class="w-5 h-5 mr-2 svg-fill-white">
                    Eliminar seleccionados ({{ count($selectedFiles) }})
                </button>
            @endif
            <label class="flex items-center gap-2 text-sm text-gray-600 cursor-pointer px-4 py-2 rounded-lg border border-gray-200 hover:bg-gray-50">
                <input type="checkbox"
                       wire:model.live="makeAllServable"
                       class="rounded">
                <span>Marcar todos como servibles</span>
            </label>
            <a href="{{ route('products.scss-cdn.bundle.upload', [$product, $bundle]) }}"
               class="flex items-center rounded-lg bg-green-600 px-4 py-2 text-white hover:bg-green-700 font-semibold">
                <img src="{{ asset('vendor/blade-untitledui-icons/upload.svg') }}" alt="Subir" class="w-5 h-5 mr-2 svg-fill-white">
                Subir ZIP
            </a>
        </div>
    </div>

    {{-- Tabla de archivos --}}
    <div class="rounded-lg bg-white border border-gray-200 overflow-hidden mb-6">
        <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
            <h3 class="text-lg font-semibold">Estructura de archivos</h3>
        </div>
        <div class="overflow-x-auto">
            <table class="min-w-full text-sm">
                <thead class="bg-gray-100 text-xs uppercase text-gray-600">
                <tr>
                    <th class="px-4 py-3 text-left w-12">
                        <input type="checkbox"
                               wire:model.live="selectAll"
                               class="rounded"
                               title="Seleccionar todos para borrado">
                    </th>
                    <th class="px-4 py-3 text-left">Ruta del archivo</th>
                    <th class="px-4 py-3 text-left w-32">Servible</th>
                    <th class="px-4 py-3 text-right">Acciones</th>
                </tr>
                </thead>
                <tbody class="divide-y">
                @foreach($files as $file)
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-3">
                            <input type="checkbox"
                                   wire:click="toggleSelectFile({{ $file->id }})"
                                   {{ in_array($file->id, $selectedFiles) ? 'checked' : '' }}
                                   class="rounded">
                        </td>
                        <td class="px-4 py-3 font-mono font-semibold text-gray-900">{{ $file->relative_path }}</td>
                        <td class="px-4 py-3">
                            <label class="flex items-center cursor-pointer" title="Marcar como servible (se servirá cuando Moodle solicite con action 'scss')">
                                <input type="checkbox"
                                       wire:click="toggleServable({{ $file->id }})"
                                       {{ $file->is_servable ? 'checked' : '' }}
                                       class="rounded">
                                <span class="ml-2 text-xs font-semibold {{ $file->is_servable ? 'text-green-600' : 'text-gray-500' }}">
                                    {{ $file->is_servable ? 'Sí' : 'No' }}
                                </span>
                            </label>
                        </td>
                        <td class="px-4 py-3 text-right">
                            <div class="flex items-center justify-end gap-2">
                                <a href="{{ route('products.scss-cdn.file.edit', [$product, $bundle, $file]) }}"
                                   class="text-orange-600 hover:text-orange-800 text-xs font-semibold">
                                    Editar
                                </a>
                                <button wire:click="deleteFile({{ $file->id }})"
                                        wire:confirm="¿Seguro que deseas eliminar este archivo?"
                                        class="text-red-600 hover:text-red-800 text-xs font-semibold">
                                    Eliminar
                                </button>
                            </div>
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
    </div>

    {{-- Archivos servibles --}}
    @if($servableFiles->count() > 0)
        <div class="rounded-lg bg-white border border-gray-200 overflow-hidden">
            <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                <h3 class="text-lg font-semibold">Archivos servibles ({{ $servableFiles->count() }})</h3>
            </div>
            <div class="p-6">
                <div class="space-y-3">
                    @foreach($servableFiles as $file)
                        <div class="rounded-lg border border-gray-200 p-4 hover:bg-gray-50">
                            <div class="mb-2 font-mono text-sm font-semibold text-gray-900">{{ $file->relative_path }}</div>
                            <div class="text-xs text-gray-600 space-y-1">
                                <div>URL: <a href="{{ $this->getFileUrl($file->id) }}" target="_blank" class="text-orange-600 hover:underline break-all">{{ $this->getFileUrl($file->id) }}</a></div>
                                @php
                                    $fileContent = \App\Services\ScssCdn\ScssCdnStorageService::getFile($product, $bundle, $file->relative_path);
                                    $fileSize = $fileContent ? strlen($fileContent) : 0;
                                @endphp
                                <div>Tamaño: {{ number_format($fileSize, 0, ',', '.') }} bytes</div>
                            </div>
                        </div>
                    @endforeach
                </div>
            </div>
        </div>
    @endif
</div>
