<script setup>
/**
 * Coach Meal Plan Templates Index
 */
import CoachLayout from '@/Layouts/CoachLayout.vue';
import { Head, Link, router } from '@inertiajs/vue3';
import { ref, watch } from 'vue';
import debounce from 'lodash/debounce';

const props = defineProps({
    templates: Object,
    filters: Object,
});

const search = ref(props.filters?.search || '');

const applyFilters = debounce(() => {
    router.get(route('coach.meal-plans.index'), {
        search: search.value || undefined,
    }, { preserveState: true, replace: true });
}, 300);

watch(search, applyFilters);

const deleteTemplate = (template) => {
    if (template.is_official) return;
    if (confirm(`¿Eliminar la plantilla "${template.name}"?`)) {
        router.delete(route('coach.meal-plans.destroy', template.id));
    }
};
</script>

<template>
    <Head title="Plantillas de Dietas" />

    <CoachLayout>
        <template #header>
            <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
                <div>
                    <h2 class="text-xl font-bold text-gray-900 dark:text-white">
                        Plantillas de Dietas
                    </h2>
                    <p class="text-sm text-gray-500 dark:text-gray-400">
                        Crea planes alimenticios reutilizables
                    </p>
                </div>
                <Link
                    :href="route('coach.meal-plans.create')"
                    class="inline-flex items-center gap-2 px-4 py-2 bg-gradient-to-r from-green-500 to-emerald-600 hover:from-green-600 hover:to-emerald-700 text-white font-medium rounded-lg transition-all"
                >
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                    </svg>
                    Nueva Plantilla
                </Link>
            </div>
        </template>

        <div class="space-y-6">
            <!-- Filters -->
            <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-100 dark:border-gray-700 p-4">
                <div class="relative max-w-md">
                    <svg class="absolute left-3 top-1/2 -translate-y-1/2 w-5 h-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
                    </svg>
                    <input
                        v-model="search"
                        type="text"
                        placeholder="Buscar plantillas..."
                        class="w-full pl-10 pr-4 py-2 rounded-lg border-gray-300 dark:border-gray-600 dark:bg-gray-700 dark:text-white focus:ring-green-500 focus:border-green-500"
                    />
                </div>
            </div>

            <!-- Templates Grid -->
            <div v-if="templates.data.length" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                <div
                    v-for="template in templates.data"
                    :key="template.id"
                    class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-100 dark:border-gray-700 hover:border-green-300 dark:hover:border-green-600 transition-colors"
                >
                    <div class="p-5">
                        <div class="flex items-start justify-between mb-3">
                            <div class="flex items-center gap-2 flex-1 min-w-0">
                                <h3 class="font-semibold text-gray-900 dark:text-white truncate">
                                    {{ template.name }}
                                </h3>
                                <span v-if="template.is_official" class="inline-flex items-center gap-1 px-2 py-0.5 text-xs font-semibold rounded-full bg-blue-100 text-blue-700 dark:bg-blue-900/40 dark:text-blue-300 whitespace-nowrap">
                                    <svg class="w-3 h-3" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M6.267 3.455a3.066 3.066 0 001.745-.723 3.066 3.066 0 013.976 0 3.066 3.066 0 001.745.723 3.066 3.066 0 012.812 2.812c.051.643.304 1.254.723 1.745a3.066 3.066 0 010 3.976 3.066 3.066 0 00-.723 1.745 3.066 3.066 0 01-2.812 2.812 3.066 3.066 0 00-1.745.723 3.066 3.066 0 01-3.976 0 3.066 3.066 0 00-1.745-.723 3.066 3.066 0 01-2.812-2.812 3.066 3.066 0 00-.723-1.745 3.066 3.066 0 010-3.976 3.066 3.066 0 00.723-1.745 3.066 3.066 0 012.812-2.812zm7.44 5.252a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd" /></svg>
                                    Oficial
                                </span>
                            </div>
                            <div class="flex items-center gap-1">
                                <Link
                                    :href="route('coach.meal-plans.edit', template.id)"
                                    class="p-2 text-gray-400 hover:text-green-600 dark:hover:text-green-400 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg transition-colors"
                                    :title="template.is_official ? 'Duplicar plantilla' : 'Editar plantilla'"
                                >
                                    <svg v-if="template.is_official" class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" />
                                    </svg>
                                    <svg v-else class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z" />
                                    </svg>
                                </Link>
                                <button
                                    v-if="!template.is_official"
                                    @click="deleteTemplate(template)"
                                    class="p-2 text-gray-400 hover:text-red-600 dark:hover:text-red-400 hover:bg-gray-100 dark:hover:bg-gray-700 rounded-lg transition-colors"
                                >
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                                    </svg>
                                </button>
                            </div>
                        </div>
                        <p v-if="template.description" class="text-sm text-gray-500 dark:text-gray-400 line-clamp-2 mb-4">
                            {{ template.description }}
                        </p>

                        <!-- Macro Targets -->
                        <div class="grid grid-cols-4 gap-2 text-center">
                            <div class="bg-gray-50 dark:bg-gray-700/50 rounded-lg p-2">
                                <div class="text-xs text-gray-500 dark:text-gray-400">Calorías</div>
                                <div class="font-semibold text-gray-900 dark:text-white text-sm">
                                    {{ template.target_calories || template.calories_target || '-' }}
                                </div>
                            </div>
                            <div class="bg-blue-50 dark:bg-blue-900/20 rounded-lg p-2">
                                <div class="text-xs text-blue-600 dark:text-blue-400">Prot</div>
                                <div class="font-semibold text-blue-700 dark:text-blue-300 text-sm">
                                    {{ template.target_proteins || template.protein_target || '-' }}g
                                </div>
                            </div>
                            <div class="bg-amber-50 dark:bg-amber-900/20 rounded-lg p-2">
                                <div class="text-xs text-amber-600 dark:text-amber-400">Carbs</div>
                                <div class="font-semibold text-amber-700 dark:text-amber-300 text-sm">
                                    {{ template.target_carbs || template.carbs_target || '-' }}g
                                </div>
                            </div>
                            <div class="bg-red-50 dark:bg-red-900/20 rounded-lg p-2">
                                <div class="text-xs text-red-600 dark:text-red-400">Grasa</div>
                                <div class="font-semibold text-red-700 dark:text-red-300 text-sm">
                                    {{ template.target_fats || template.fat_target || '-' }}g
                                </div>
                            </div>
                        </div>

                        <div class="mt-3 flex items-center text-sm text-gray-500 dark:text-gray-400">
                            <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.747 0 3.332.477 4.5 1.253v13C19.832 18.477 18.247 18 16.5 18c-1.746 0-3.332.477-4.5 1.253" />
                            </svg>
                            {{ template.meal_items?.length || 0 }} alimentos
                        </div>
                    </div>
                </div>
            </div>

            <!-- Empty State -->
            <div v-else class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-100 dark:border-gray-700 p-12 text-center">
                <div class="w-16 h-16 bg-gray-100 dark:bg-gray-700 rounded-full flex items-center justify-center mx-auto mb-4">
                    <svg class="w-8 h-8 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
                    </svg>
                </div>
                <h3 class="text-lg font-medium text-gray-900 dark:text-white mb-2">
                    No hay plantillas de dietas
                </h3>
                <p class="text-gray-500 dark:text-gray-400 mb-4">
                    Crea plantillas para asignarlas rápidamente a tus clientes
                </p>
                <Link
                    :href="route('coach.meal-plans.create')"
                    class="inline-flex items-center gap-2 px-4 py-2 bg-gradient-to-r from-green-500 to-emerald-600 hover:from-green-600 hover:to-emerald-700 text-white font-medium rounded-lg transition-all"
                >
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4" />
                    </svg>
                    Crear Primera Plantilla
                </Link>
            </div>

            <!-- Pagination -->
            <div v-if="templates.links && templates.links.length > 3" class="flex justify-center gap-1">
                <Link
                    v-for="link in templates.links"
                    :key="link.label"
                    :href="link.url"
                    v-html="link.label"
                    class="px-3 py-2 rounded-lg text-sm transition-colors"
                    :class="{
                        'bg-green-500 text-white': link.active,
                        'bg-white dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700': !link.active && link.url,
                        'text-gray-400 cursor-not-allowed': !link.url,
                    }"
                />
            </div>
        </div>
    </CoachLayout>
</template>
