<script setup>
import { ref, computed, watch } from 'vue'
import { router } from '@inertiajs/vue3'
import axios from 'axios'
import { VueDraggable } from 'vue-draggable-plus'
import ConfirmDialog from '@/Components/ConfirmDialog.vue'
import LoadingSpinner from '@/Components/LoadingSpinner.vue'

const props = defineProps({
    slides: {
        type: Array,
        default: () => [],
    },
    type: {
        type: String,
        required: true,
        validator: (v) => ['information', 'moderation', 'sermon'].includes(v),
    },
    showExpireDate: {
        type: Boolean,
        default: false,
    },
    showUploader: {
        type: Boolean,
        default: false,
    },
})

const emit = defineEmits(['deleted', 'updated'])

// Delete confirmation state
const confirmingDelete = ref(false)
const slideToDelete = ref(null)
const deleting = ref(false)

// Inline expire date editing
const editingExpireId = ref(null)
const editingExpireValue = ref('')

// Preview popup state (#7)
const previewSlide = ref(null)

// Draggable local slides (#9)
const localSlides = ref([])

watch(() => props.slides, (newSlides) => {
    localSlides.value = [...newSlides].sort((a, b) => {
        const orderA = a.sort_order ?? 0
        const orderB = b.sort_order ?? 0
        if (orderA !== orderB) return orderA - orderB
        return new Date(a.uploaded_at) - new Date(b.uploaded_at)
    })
}, { immediate: true, deep: true })

// Sort slides by sort_order ascending (#5) — kept for non-draggable contexts
const sortedSlides = computed(() => {
    return [...props.slides].sort((a, b) => {
        const orderA = a.sort_order ?? 0
        const orderB = b.sort_order ?? 0
        if (orderA !== orderB) return orderA - orderB
        return new Date(a.uploaded_at) - new Date(b.uploaded_at)
    })
})

function thumbnailUrl(slide) {
    if (!slide.thumbnail_filename) return null
    return `/storage/${slide.thumbnail_filename}`
}

function fullImageUrl(slide) {
    if (!slide.stored_filename) return null
    return `/storage/${slide.stored_filename}`
}

function formatDate(dateStr) {
    if (!dateStr) return '\u2014'
    const d = new Date(dateStr)
    return d.toLocaleDateString('de-DE', {
        day: '2-digit',
        month: '2-digit',
        year: 'numeric',
    })
}

function formatDateTime(dateStr) {
    if (!dateStr) return '\u2014'
    const d = new Date(dateStr)
    return d.toLocaleDateString('de-DE', {
        day: '2-digit',
        month: '2-digit',
        year: 'numeric',
        hour: '2-digit',
        minute: '2-digit',
    })
}

// Delete slide
function promptDelete(slide) {
    slideToDelete.value = slide
    confirmingDelete.value = true
}

function cancelDelete() {
    confirmingDelete.value = false
    slideToDelete.value = null
}

function confirmDelete() {
    if (!slideToDelete.value) return
    deleting.value = true

    axios.delete(route('slides.destroy', slideToDelete.value.id))
        .then(() => {
            confirmingDelete.value = false
            slideToDelete.value = null
            deleting.value = false
            emit('deleted')
            router.reload({ preserveScroll: true })
        })
        .catch(() => {
            deleting.value = false
        })
}

// Expire date editing
function startEditExpire(slide) {
    editingExpireId.value = slide.id
    editingExpireValue.value = slide.expire_date
        ? new Date(slide.expire_date).toISOString().split('T')[0]
        : ''
}

function saveExpireDate(slide) {
    router.patch(
        route('slides.update-expire-date', slide.id),
        { expire_date: editingExpireValue.value || null },
        {
            preserveScroll: true,
            preserveState: true,
            onSuccess: () => {
                editingExpireId.value = null
                emit('updated')
            },
        },
    )
}

function cancelEditExpire() {
    editingExpireId.value = null
    editingExpireValue.value = ''
}

function isExpireSoon(expireDate) {
    if (!expireDate) return false
    const diff = new Date(expireDate) - new Date()
    return diff > 0 && diff < 7 * 24 * 60 * 60 * 1000 // 7 days
}

function isExpired(expireDate) {
    if (!expireDate) return false
    return new Date(expireDate) < new Date()
}

// Preview popup (#7)
function openPreview(slide) {
    previewSlide.value = slide
}

function closePreview() {
    previewSlide.value = null
}

function deleteFromPreview() {
    if (previewSlide.value) {
        promptDelete(previewSlide.value)
        closePreview()
    }
}

// Drag reorder (#9)
function onDragEnd() {
    const reordered = localSlides.value.map((slide, index) => ({
        id: slide.id,
        sort_order: index + 1,
    }))

    axios.post(route('slides.reorder'), { slides: reordered })
        .then(() => emit('updated'))
        .catch(console.error)
}
</script>

<template>
    <div data-testid="slide-grid" class="slide-grid">
        <!-- Empty state (only when no slides AND no uploader) -->
        <div
            v-if="localSlides.length === 0 && !showUploader"
            class="flex flex-col items-center justify-center rounded-xl border-2 border-dashed border-gray-200 bg-gray-50/50 py-10"
        >
            <svg class="h-10 w-10 text-gray-300 mb-2" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1">
                <path stroke-linecap="round" stroke-linejoin="round" d="M2.25 15.75l5.159-5.159a2.25 2.25 0 013.182 0l5.159 5.159m-1.5-1.5l1.409-1.409a2.25 2.25 0 013.182 0l2.909 2.909M3.75 21h16.5a2.25 2.25 0 002.25-2.25V5.25a2.25 2.25 0 00-2.25-2.25H3.75A2.25 2.25 0 001.5 5.25v13.5A2.25 2.25 0 003.75 21z" />
            </svg>
            <p class="text-sm text-gray-400">Noch keine Folien vorhanden</p>
        </div>

        <!-- Grid of thumbnails with drag'n'drop (#9) -->
         <VueDraggable
             v-if="localSlides.length > 0 || showUploader"
             v-model="localSlides"
             class="grid grid-cols-1 gap-5 sm:grid-cols-2 lg:grid-cols-3"
             @end="onDragEnd"
             item-key="id"
             filter=".no-drag"
             :prevent-on-filter="false"
             ghost-class="slide-drag-ghost"
             chosen-class="slide-drag-chosen"
             drag-class="slide-drag-active"
         >
            <div
                v-for="slide in localSlides"
                :key="slide.id"
                class="slide-card group relative overflow-hidden rounded-xl border border-gray-200/80 bg-white shadow-sm transition-all duration-300 hover:shadow-lg hover:border-gray-300 hover:-translate-y-0.5 cursor-grab active:cursor-grabbing"
            >
                <!-- PPT processing indicator -->
                <div
                    v-if="slide._processing"
                    class="absolute inset-0 z-10 flex flex-col items-center justify-center bg-white/90 backdrop-blur-sm"
                >
                    <LoadingSpinner size="md" />
                    <span class="mt-2 text-xs font-medium text-gray-500">Verarbeitung l&auml;uft...</span>
                </div>

                <!-- Thumbnail -->
                <div class="relative aspect-video overflow-hidden bg-gray-900">
                    <img
                        v-if="thumbnailUrl(slide)"
                        :src="thumbnailUrl(slide)"
                        :alt="slide.original_filename"
                        class="h-full w-full object-contain transition-transform duration-500 group-hover:scale-105"
                        loading="lazy"
                    />
                    <div
                        v-else
                        class="flex h-full w-full items-center justify-center bg-gradient-to-br from-gray-800 to-gray-900"
                    >
                        <svg class="h-10 w-10 text-gray-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M2.25 15.75l5.159-5.159a2.25 2.25 0 013.182 0l5.159 5.159m-1.5-1.5l1.409-1.409a2.25 2.25 0 013.182 0l2.909 2.909M3.75 21h16.5a2.25 2.25 0 002.25-2.25V5.25a2.25 2.25 0 00-2.25-2.25H3.75A2.25 2.25 0 001.5 5.25v13.5A2.25 2.25 0 003.75 21z" />
                        </svg>
                    </div>

                    <!-- Delete button — LEFT side, always visible -->
                    <button
                        data-testid="slide-grid-delete-button"
                        @click.stop="promptDelete(slide)"
                        class="absolute left-2.5 top-2.5 flex h-8 w-8 items-center justify-center rounded-lg bg-black/60 text-white shadow-sm backdrop-blur-sm transition-all duration-200 hover:bg-red-600 hover:scale-110"
                        title="L&ouml;schen"
                    >
                        <svg class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" />
                        </svg>
                    </button>

                    <!-- Preview button — RIGHT side (#7) -->
                    <button
                        v-if="fullImageUrl(slide)"
                        data-testid="slide-grid-preview-button"
                        @click.stop="openPreview(slide)"
                        class="absolute right-2.5 top-2.5 flex h-8 w-8 items-center justify-center rounded-lg bg-black/60 text-white shadow-sm backdrop-blur-sm transition-all duration-200 hover:bg-white/90 hover:text-gray-800 hover:scale-110"
                        title="Vorschau"
                    >
                        <svg class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M2.036 12.322a1.012 1.012 0 010-.639C3.423 7.51 7.36 4.5 12 4.5c4.638 0 8.573 3.007 9.963 7.178.07.207.07.431 0 .639C20.577 16.49 16.64 19.5 12 19.5c-4.638 0-8.573-3.007-9.963-7.178z" />
                            <path stroke-linecap="round" stroke-linejoin="round" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                        </svg>
                    </button>
                </div>

                <!-- Metadata -->
                <div class="px-3.5 py-3">
                    <!-- Upload info (muted) -->
                    <div class="flex items-center gap-1.5 text-xs text-gray-400">
                        <svg class="h-3.5 w-3.5 shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M12 6v6h4.5m4.5 0a9 9 0 11-18 0 9 9 0 0118 0z" />
                        </svg>
                        <span class="truncate">{{ formatDateTime(slide.uploaded_at) }}</span>
                    </div>
                    <div
                        v-if="slide.uploader_name"
                        class="mt-0.5 flex items-center gap-1.5 text-xs text-gray-400"
                    >
                        <svg class="h-3.5 w-3.5 shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M15.75 6a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0zM4.501 20.118a7.5 7.5 0 0114.998 0" />
                        </svg>
                        <span class="truncate">{{ slide.uploader_name }}</span>
                    </div>

                    <!-- Expire date (prominent for information slides) -->
                    <div
                        v-if="showExpireDate"
                        class="mt-2"
                    >
                        <!-- Display mode -->
                        <div
                            v-if="editingExpireId !== slide.id"
                            class="flex items-center gap-1.5 cursor-pointer rounded-lg px-2 py-1 -mx-1 transition-colors hover:bg-gray-50"
                            @click="startEditExpire(slide)"
                            :title="'Ablaufdatum &auml;ndern'"
                        >
                            <svg
                                class="h-3.5 w-3.5 shrink-0"
                                :class="{
                                    'text-red-500': isExpired(slide.expire_date),
                                    'text-amber-500': isExpireSoon(slide.expire_date),
                                    'text-emerald-500': slide.expire_date && !isExpired(slide.expire_date) && !isExpireSoon(slide.expire_date),
                                    'text-gray-300': !slide.expire_date,
                                }"
                                fill="none"
                                viewBox="0 0 24 24"
                                stroke="currentColor"
                                stroke-width="2"
                            >
                                <path stroke-linecap="round" stroke-linejoin="round" d="M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 012.25-2.25h13.5A2.25 2.25 0 0121 7.5v11.25m-18 0A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75m-18 0v-7.5A2.25 2.25 0 015.25 9h13.5A2.25 2.25 0 0121 11.25v7.5" />
                            </svg>
                            <span
                                class="text-xs font-semibold"
                                :class="{
                                    'text-red-600': isExpired(slide.expire_date),
                                    'text-amber-600': isExpireSoon(slide.expire_date),
                                    'text-gray-700': slide.expire_date && !isExpired(slide.expire_date) && !isExpireSoon(slide.expire_date),
                                    'text-gray-400 font-normal': !slide.expire_date,
                                }"
                            >
                                {{ slide.expire_date ? formatDate(slide.expire_date) : 'Kein Ablaufdatum' }}
                            </span>

                            <!-- Tiny edit icon -->
                            <svg class="h-3 w-3 text-gray-300 opacity-0 group-hover:opacity-100 transition-opacity" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L10.582 16.07a4.5 4.5 0 01-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 011.13-1.897l8.932-8.931zm0 0L19.5 7.125" />
                            </svg>
                        </div>

                        <!-- Edit mode -->
                        <div
                            v-else
                            class="flex items-center gap-1.5"
                        >
                            <input
                                data-testid="slide-grid-expire-input"
                                v-model="editingExpireValue"
                                type="date"
                                class="w-full rounded-md border border-gray-300 bg-white px-2 py-1 text-xs text-gray-700 shadow-sm focus:border-amber-400 focus:outline-none focus:ring-1 focus:ring-amber-400/40"
                                @keydown.enter="saveExpireDate(slide)"
                                @keydown.escape="cancelEditExpire"
                            />
                            <button
                                data-testid="slide-grid-expire-save"
                                @click="saveExpireDate(slide)"
                                class="flex h-6 w-6 shrink-0 items-center justify-center rounded-md bg-amber-500 text-white shadow-sm transition hover:bg-amber-600"
                                title="Speichern"
                            >
                                <svg class="h-3 w-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="3">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5" />
                                </svg>
                            </button>
                            <button
                                data-testid="slide-grid-expire-cancel"
                                @click="cancelEditExpire"
                                class="flex h-6 w-6 shrink-0 items-center justify-center rounded-md border border-gray-200 bg-white text-gray-500 shadow-sm transition hover:bg-gray-50"
                                title="Abbrechen"
                            >
                                <svg class="h-3 w-3" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="3">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
                                </svg>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <div
                v-if="showUploader"
                class="no-drag slide-upload-card relative overflow-hidden rounded-xl border-2 border-dashed border-gray-300 bg-gradient-to-br from-gray-50/80 to-amber-50/40 transition-all duration-300 hover:border-amber-400 hover:shadow-md hover:from-amber-50/60 hover:to-orange-50/40"
            >
                <div class="flex h-full items-center justify-center">
                    <slot name="upload-card">
                        <div class="flex flex-col items-center gap-2 text-gray-400">
                            <svg class="h-10 w-10" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
                            </svg>
                            <span class="text-sm font-medium">Folien hinzuf&uuml;gen</span>
                        </div>
                    </slot>
                </div>
            </div>
        </VueDraggable>

        <!-- Slide preview popup (#7) -->
        <Teleport to="body">
            <Transition
                enter-active-class="transition duration-200 ease-out"
                enter-from-class="opacity-0"
                enter-to-class="opacity-100"
                leave-active-class="transition duration-150 ease-in"
                leave-from-class="opacity-100"
                leave-to-class="opacity-0"
            >
                <div
                    v-if="previewSlide"
                    class="fixed inset-0 z-[60] flex items-center justify-center bg-black/70 backdrop-blur-sm"
                    @click.self="closePreview"
                >
                    <div class="relative max-h-[90vh] max-w-[90vw]">
                        <img
                            :src="fullImageUrl(previewSlide)"
                            :alt="previewSlide.original_filename"
                            class="max-h-[85vh] max-w-[85vw] rounded-lg object-contain shadow-2xl"
                        />
                        <div class="absolute -top-3 -right-3 flex gap-2">
                            <button
                                @click="deleteFromPreview"
                                class="flex h-10 w-10 items-center justify-center rounded-full bg-red-600 text-white shadow-lg transition hover:bg-red-700"
                                title="L&ouml;schen"
                            >
                                <svg class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" />
                                </svg>
                            </button>
                            <button
                                @click="closePreview"
                                class="flex h-10 w-10 items-center justify-center rounded-full bg-gray-800 text-white shadow-lg transition hover:bg-gray-700"
                                title="Schliessen"
                            >
                                <svg class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
                                </svg>
                            </button>
                        </div>
                    </div>
                </div>
            </Transition>
        </Teleport>

        <!-- Delete confirmation dialog (#3: removed Teleport wrapper — dialog uses showModal() top layer) -->
        <ConfirmDialog
            :show="confirmingDelete"
            title="Folie l&ouml;schen?"
            :message="`M&ouml;chtest du die Folie '${slideToDelete?.original_filename || ''}' wirklich l&ouml;schen?`"
            confirm-label="L&ouml;schen"
            cancel-label="Abbrechen"
            variant="danger"
            @confirm="confirmDelete"
            @cancel="cancelDelete"
        />
    </div>
</template>

<style scoped>
.slide-card {
    contain: layout;
}

.slide-card img {
    image-rendering: auto;
}

.slide-upload-card {
    min-height: 0;
}

/* Drag highlight styles */
.slide-drag-ghost {
    opacity: 0.4;
    border: 2px dashed rgb(99, 102, 241);
}

.slide-drag-chosen {
    ring: 2px;
    ring-color: rgb(99, 102, 241);
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    transform: scale(1.05);
}

.slide-drag-active {
    opacity: 0.8;
    transform: rotate(2deg);
}
</style>
