@extends('layouts.self-paced-dashboard')

@section('title', 'Downloads — ' . $course->title)
@section('page-title', 'Course Downloads')
@section('page-subtitle', $course->title)

@section('hero-actions')
    <a href="{{ route('self-paced.dashboard.courses.show', $course) }}"
       class="inline-flex items-center gap-2 px-4 py-2 bg-white/10 hover:bg-white/20 text-white text-sm font-medium rounded-lg transition border border-white/30">
        <i class="fas fa-arrow-left"></i> Back to Course
    </a>
@endsection

@section('content')
<div x-data="{ deleteModal: { open: false, id: null, title: '' } }">

    {{-- Breadcrumb --}}
    <nav class="flex items-center gap-2 text-xs text-gray-400 mb-5 flex-wrap">
        <a href="{{ route('self-paced.dashboard.index') }}" class="hover:text-indigo-600 transition">Dashboard</a>
        <i class="fas fa-chevron-right text-[10px]"></i>
        <a href="{{ route('self-paced.dashboard.courses.index') }}" class="hover:text-indigo-600 transition">Courses</a>
        <i class="fas fa-chevron-right text-[10px]"></i>
        <a href="{{ route('self-paced.dashboard.courses.show', $course) }}" class="hover:text-indigo-600 transition">{{ Str::limit($course->title, 40) }}</a>
        <i class="fas fa-chevron-right text-[10px]"></i>
        <span class="text-gray-600 font-medium">Downloads</span>
    </nav>

    {{-- Upload Form --}}
    @can('create self-paced courses')
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-6 mb-5">
        <h2 class="text-sm font-semibold text-gray-800 mb-4 flex items-center gap-2">
            <i class="fas fa-file-download text-indigo-500"></i> Upload New Download
        </h2>

        <form action="{{ route('self-paced.dashboard.courses.downloads.store', $course) }}"
              method="POST"
              enctype="multipart/form-data">
            @csrf

            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">

                {{-- Title --}}
                <div>
                    <label class="block text-xs font-semibold text-gray-600 mb-1.5">
                        <i class="fas fa-tag text-gray-400 mr-1"></i> Title <span class="text-red-500">*</span>
                    </label>
                    <input type="text"
                           name="title"
                           value="{{ old('title') }}"
                           placeholder="e.g. Course Handbook"
                           class="w-full px-3 py-2 text-sm border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-300 focus:border-indigo-400 transition @error('title') border-red-400 @enderror">
                    @error('title')
                        <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                    @enderror
                </div>

                {{-- Topic (optional) --}}
                <div>
                    <label class="block text-xs font-semibold text-gray-600 mb-1.5">
                        <i class="fas fa-list text-gray-400 mr-1"></i> Attach to Topic
                        <span class="text-gray-400 font-normal">(leave blank for course-level)</span>
                    </label>
                    <select name="topic_id"
                            class="w-full px-3 py-2 text-sm border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-300 focus:border-indigo-400 transition bg-white @error('topic_id') border-red-400 @enderror">
                        <option value="">— Course-level download —</option>
                        @foreach($topics as $topic)
                            <option value="{{ $topic->id }}" {{ old('topic_id') == $topic->id ? 'selected' : '' }}>
                                {{ $topic->title }}
                            </option>
                        @endforeach
                    </select>
                    @error('topic_id')
                        <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                    @enderror
                </div>

                {{-- File --}}
                <div class="sm:col-span-2">
                    <label class="block text-xs font-semibold text-gray-600 mb-1.5">
                        <i class="fas fa-file-download text-gray-400 mr-1"></i> File <span class="text-red-500">*</span>
                    </label>
                    <input type="file"
                           name="download_file"
                           class="w-full px-3 py-2 text-sm border border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-300 focus:border-indigo-400 transition @error('download_file') border-red-400 @enderror">
                    <p class="text-xs text-gray-400 mt-1">Allowed: PDF, Excel (xlsx/xls), Word (doc/docx), ZIP, CSV. Max 500 MB.</p>
                    @error('download_file')
                        <p class="text-red-500 text-xs mt-1">{{ $message }}</p>
                    @enderror
                </div>
            </div>

            <div class="mt-4 flex justify-end">
                <button type="submit"
                        class="inline-flex items-center gap-2 px-5 py-2 bg-indigo-600 hover:bg-indigo-700 text-white text-sm font-semibold rounded-lg transition shadow-sm">
                    <i class="fas fa-upload"></i> Upload Download
                </button>
            </div>
        </form>
    </div>
    @endcan

    {{-- Course-Level Downloads --}}
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden mb-5">
        <div class="flex items-center justify-between px-5 py-4 border-b border-gray-100">
            <h2 class="text-sm font-semibold text-gray-800 flex items-center gap-2">
                <i class="fas fa-book-open text-indigo-500"></i>
                Course-Level Downloads
                <span class="ml-1 px-2 py-0.5 bg-indigo-100 text-indigo-700 text-xs rounded-full font-medium">
                    {{ $courseDownloads->count() }}
                </span>
            </h2>
        </div>

        @if($courseDownloads->isEmpty())
        <div class="px-5 py-12 text-center">
            <i class="fas fa-file-download text-4xl text-gray-200 mb-3 block"></i>
            <p class="text-gray-400 text-sm">No course-level downloads yet.</p>
            <p class="text-gray-300 text-xs mt-1">Upload a file without selecting a topic to add one here.</p>
        </div>
        @else
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead>
                    <tr>
                        <th class="bg-indigo-50 text-indigo-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide w-10">#</th>
                        <th class="bg-purple-50 text-purple-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">Title</th>
                        <th class="bg-blue-50 text-blue-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">File Name</th>
                        <th class="bg-green-50 text-green-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">File Size</th>
                        <th class="bg-gray-50 text-gray-600 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-50">
                    @foreach($courseDownloads as $i => $download)
                    <tr class="odd:bg-white even:bg-gray-50 hover:bg-indigo-50/60 transition-colors border-b border-gray-100 last:border-0">
                        <td class="px-4 py-3 text-gray-400 text-xs font-medium">{{ $i + 1 }}</td>
                        <td class="px-4 py-3">
                            <p class="font-semibold text-gray-800 text-sm">{{ $download->title }}</p>
                            <p class="text-gray-400 text-xs mt-0.5">{{ $download->created_at?->format('d M Y') }}</p>
                        </td>
                        <td class="px-4 py-3 text-xs text-gray-600 max-w-[220px] truncate">
                            <i class="fas fa-file text-gray-300 mr-1"></i>{{ $download->file_name }}
                        </td>
                        <td class="px-4 py-3 text-xs text-gray-600">
                            {{ $download->getFormattedFileSize() }}
                        </td>
                        <td class="px-4 py-3">
                            @can('delete self-paced courses')
                            <button type="button"
                                    @click="deleteModal = { open: true, id: {{ $download->id }}, title: '{{ addslashes($download->title) }}' }"
                                    class="w-7 h-7 flex items-center justify-center rounded-lg bg-red-50 hover:bg-red-100 text-red-600 transition">
                                <i class="fas fa-trash text-xs"></i>
                            </button>
                            <form id="delete-download-form-{{ $download->id }}"
                                  action="{{ route('self-paced.dashboard.courses.downloads.destroy', [$course, $download]) }}"
                                  method="POST" class="hidden">
                                @csrf @method('DELETE')
                            </form>
                            @endcan
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        @endif
    </div>

    {{-- Topic-Level Downloads --}}
    @foreach($topics as $topic)
    @if($topic->downloads->isNotEmpty())
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden mb-5">
        <div class="flex items-center justify-between px-5 py-4 border-b border-gray-100">
            <h2 class="text-sm font-semibold text-gray-800 flex items-center gap-2">
                <i class="fas fa-list-ul text-teal-500"></i>
                {{ $topic->title }}
                <span class="ml-1 px-2 py-0.5 bg-teal-100 text-teal-700 text-xs rounded-full font-medium">
                    {{ $topic->downloads->count() }} download(s)
                </span>
            </h2>
        </div>

        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead>
                    <tr>
                        <th class="bg-indigo-50 text-indigo-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide w-10">#</th>
                        <th class="bg-purple-50 text-purple-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">Title</th>
                        <th class="bg-blue-50 text-blue-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">File Name</th>
                        <th class="bg-green-50 text-green-700 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">File Size</th>
                        <th class="bg-gray-50 text-gray-600 font-semibold text-left px-4 py-3 text-xs uppercase tracking-wide">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-50">
                    @foreach($topic->downloads as $i => $download)
                    <tr class="odd:bg-white even:bg-gray-50 hover:bg-indigo-50/60 transition-colors border-b border-gray-100 last:border-0">
                        <td class="px-4 py-3 text-gray-400 text-xs font-medium">{{ $i + 1 }}</td>
                        <td class="px-4 py-3">
                            <p class="font-semibold text-gray-800 text-sm">{{ $download->title }}</p>
                            <p class="text-gray-400 text-xs mt-0.5">{{ $download->created_at?->format('d M Y') }}</p>
                        </td>
                        <td class="px-4 py-3 text-xs text-gray-600 max-w-[220px] truncate">
                            <i class="fas fa-file text-gray-300 mr-1"></i>{{ $download->file_name }}
                        </td>
                        <td class="px-4 py-3 text-xs text-gray-600">
                            {{ $download->getFormattedFileSize() }}
                        </td>
                        <td class="px-4 py-3">
                            @can('delete self-paced courses')
                            <button type="button"
                                    @click="deleteModal = { open: true, id: {{ $download->id }}, title: '{{ addslashes($download->title) }}' }"
                                    class="w-7 h-7 flex items-center justify-center rounded-lg bg-red-50 hover:bg-red-100 text-red-600 transition">
                                <i class="fas fa-trash text-xs"></i>
                            </button>
                            <form id="delete-download-form-{{ $download->id }}"
                                  action="{{ route('self-paced.dashboard.courses.downloads.destroy', [$course, $download]) }}"
                                  method="POST" class="hidden">
                                @csrf @method('DELETE')
                            </form>
                            @endcan
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
    @endif
    @endforeach

    {{-- Empty state when no topic downloads exist at all --}}
    @if($topics->every(fn($t) => $t->downloads->isEmpty()) && $courseDownloads->isEmpty())
    {{-- Already handled by course-level empty state above --}}
    @elseif($topics->every(fn($t) => $t->downloads->isEmpty()) && $courseDownloads->isNotEmpty())
    <div class="bg-white rounded-xl shadow-sm border border-gray-100 p-8 text-center">
        <i class="fas fa-list-ul text-4xl text-gray-200 mb-3 block"></i>
        <p class="text-gray-400 text-sm">No topic-level downloads yet.</p>
        <p class="text-gray-300 text-xs mt-1">Select a topic when uploading to attach a download to a specific topic.</p>
    </div>
    @endif

    {{-- Delete Confirmation Modal --}}
    <div x-show="deleteModal.open"
         x-cloak
         class="fixed inset-0 z-50 flex items-center justify-center p-4"
         x-transition:enter="transition ease-out duration-200"
         x-transition:enter-start="opacity-0"
         x-transition:enter-end="opacity-100">
        <div class="absolute inset-0 bg-black/40" @click="deleteModal.open = false"></div>
        <div class="relative bg-white rounded-2xl shadow-2xl w-full max-w-md overflow-hidden z-10">
            <div class="flex items-center justify-between px-6 py-4 bg-gradient-to-r from-red-600 to-red-700 rounded-t-2xl">
                <h3 class="text-base font-semibold text-white flex items-center gap-2">
                    <i class="fas fa-trash"></i> Delete Download
                </h3>
                <button type="button" @click="deleteModal.open = false" class="text-white/80 hover:text-white transition">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            <div class="p-6">
                <p class="text-sm text-gray-500 mb-3">The file will be permanently removed.</p>
                <p class="text-sm text-gray-600 mb-5">
                    Are you sure you want to delete <strong x-text="deleteModal.title"></strong>?
                </p>
                <div class="flex gap-3 justify-end">
                    <button @click="deleteModal.open = false"
                            class="px-4 py-2 text-sm font-medium text-gray-600 bg-gray-100 hover:bg-gray-200 rounded-lg transition">
                        Cancel
                    </button>
                    <button @click="document.getElementById('delete-download-form-' + deleteModal.id)?.submit()"
                            class="px-4 py-2 text-sm font-medium text-white bg-red-600 hover:bg-red-700 rounded-lg transition">
                        <i class="fas fa-trash mr-1.5"></i> Delete
                    </button>
                </div>
            </div>
        </div>
    </div>

</div>
@endsection
