<script setup>
import { Head, useForm } from '@inertiajs/vue3';
import CoachLayout from '@/Layouts/CoachLayout.vue';
import { ref } from 'vue';

const props = defineProps({
    testimonials: Array,
    canSubmit: Boolean,
});

const form = useForm({
    content: '',
    rating: 5,
    avatar: null,
});

const previewAvatar = ref(null);

const setRating = (rating) => {
    form.rating = rating;
};

const handleAvatarChange = (e) => {
    const file = e.target.files[0];
    if (file) {
        form.avatar = file;
        previewAvatar.value = URL.createObjectURL(file);
    }
};

const submit = () => {
    form.post(route('coach.testimonials.store'), {
        preserveScroll: true,
        onSuccess: () => {
            form.reset();
            previewAvatar.value = null;
        },
    });
};

const deleteTestimonial = (id) => {
    if (confirm('¿Estás seguro de eliminar este testimonio?')) {
        form.delete(route('coach.testimonials.destroy', id), {
            preserveScroll: true,
        });
    }
};

const getStatusColor = (status) => {
    return {
        pending: 'bg-yellow-100 text-yellow-800 border-yellow-200',
        approved: 'bg-green-100 text-green-800 border-green-200',
        rejected: 'bg-red-100 text-red-800 border-red-200',
    }[status] || 'bg-gray-100 text-gray-800';
};

const getStatusLabel = (status) => {
    return {
        pending: 'Pendiente de revisión',
        approved: 'Aprobado ✓',
        rejected: 'Rechazado',
    }[status] || status;
};
</script>

<template>
    <Head title="Mis Testimonios" />

    <CoachLayout>
        <template #header>
            <h2 class="font-semibold text-xl text-gray-800 dark:text-gray-200 leading-tight">
                Mis Testimonios
            </h2>
        </template>

        <div class="py-12">
            <div class="max-w-4xl mx-auto sm:px-6 lg:px-8 space-y-8">
                
                <!-- Submit New Testimonial -->
                <div v-if="canSubmit" class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-2xl">
                    <div class="p-6 lg:p-8">
                        <div class="flex items-center gap-3 mb-6">
                            <div class="w-12 h-12 rounded-xl bg-gradient-to-br from-green-400 to-emerald-500 flex items-center justify-center">
                                <svg class="w-6 h-6 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
                                </svg>
                            </div>
                            <div>
                                <h3 class="text-lg font-bold text-gray-900 dark:text-white">
                                    Comparte tu experiencia
                                </h3>
                                <p class="text-sm text-gray-500 dark:text-gray-400">
                                    Tu testimonio podría aparecer en nuestra página principal
                                </p>
                            </div>
                        </div>

                        <form @submit.prevent="submit" class="space-y-6">
                            <!-- Rating -->
                            <div>
                                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    Tu valoración
                                </label>
                                <div class="flex gap-1">
                                    <button
                                        v-for="i in 5"
                                        :key="i"
                                        type="button"
                                        @click="setRating(i)"
                                        class="focus:outline-none transition-transform hover:scale-110"
                                    >
                                        <svg 
                                            class="w-8 h-8 transition-colors"
                                            :class="i <= form.rating ? 'text-yellow-400' : 'text-gray-300 dark:text-gray-600'"
                                            fill="currentColor" 
                                            viewBox="0 0 20 20"
                                        >
                                            <path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z" />
                                        </svg>
                                    </button>
                                </div>
                                <p v-if="form.errors.rating" class="mt-1 text-sm text-red-600">{{ form.errors.rating }}</p>
                            </div>

                            <!-- Content -->
                            <div>
                                <label for="content" class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    Tu testimonio
                                </label>
                                <textarea
                                    id="content"
                                    v-model="form.content"
                                    rows="4"
                                    class="w-full rounded-xl border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 focus:border-green-500 focus:ring-green-500 shadow-sm"
                                    placeholder="Cuéntanos cómo CoachPro ha ayudado a tu negocio de coaching..."
                                ></textarea>
                                <div class="flex justify-between mt-1">
                                    <p v-if="form.errors.content" class="text-sm text-red-600">{{ form.errors.content }}</p>
                                    <p class="text-sm text-gray-500 ml-auto">
                                        {{ form.content.length }}/500 caracteres
                                    </p>
                                </div>
                            </div>

                            <!-- Avatar (optional) -->
                            <div>
                                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                                    Foto (opcional)
                                </label>
                                <div class="flex items-center gap-4">
                                    <div class="relative">
                                        <div 
                                            v-if="previewAvatar"
                                            class="w-16 h-16 rounded-full overflow-hidden border-2 border-green-500"
                                        >
                                            <img :src="previewAvatar" class="w-full h-full object-cover" />
                                        </div>
                                        <div 
                                            v-else 
                                            class="w-16 h-16 rounded-full bg-gray-200 dark:bg-gray-700 flex items-center justify-center"
                                        >
                                            <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="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
                                            </svg>
                                        </div>
                                    </div>
                                    <label class="cursor-pointer px-4 py-2 bg-gray-100 dark:bg-gray-700 rounded-lg text-sm font-medium text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-600 transition">
                                        <span>Subir foto</span>
                                        <input 
                                            type="file" 
                                            class="hidden" 
                                            accept="image/*"
                                            @change="handleAvatarChange"
                                        />
                                    </label>
                                </div>
                                <p v-if="form.errors.avatar" class="mt-1 text-sm text-red-600">{{ form.errors.avatar }}</p>
                            </div>

                            <!-- Submit -->
                            <div class="flex justify-end">
                                <button
                                    type="submit"
                                    :disabled="form.processing || form.content.length < 50"
                                    class="px-6 py-3 bg-gradient-to-r from-green-500 to-emerald-600 text-white font-semibold rounded-xl hover:from-green-600 hover:to-emerald-700 focus:outline-none focus:ring-2 focus:ring-green-500 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed transition-all"
                                >
                                    <span v-if="form.processing">Enviando...</span>
                                    <span v-else>Enviar Testimonio</span>
                                </button>
                            </div>
                        </form>
                    </div>
                </div>

                <!-- Info when can't submit -->
                <div v-else class="bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-2xl p-6">
                    <div class="flex items-start gap-3">
                        <svg class="w-6 h-6 text-blue-500 flex-shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                        </svg>
                        <div>
                            <h4 class="font-semibold text-blue-800 dark:text-blue-300">Ya tienes un testimonio enviado</h4>
                            <p class="text-sm text-blue-700 dark:text-blue-400 mt-1">
                                Solo puedes tener un testimonio activo o pendiente de revisión. Si tu testimonio fue rechazado, puedes eliminarlo y enviar uno nuevo.
                            </p>
                        </div>
                    </div>
                </div>

                <!-- Existing Testimonials -->
                <div v-if="testimonials.length > 0" class="space-y-4">
                    <h3 class="text-lg font-bold text-gray-900 dark:text-white">
                        Historial de testimonios
                    </h3>

                    <div 
                        v-for="testimonial in testimonials" 
                        :key="testimonial.id"
                        class="bg-white dark:bg-gray-800 rounded-2xl shadow-sm p-6"
                    >
                        <div class="flex items-start justify-between mb-4">
                            <div class="flex gap-1">
                                <svg 
                                    v-for="i in testimonial.rating" 
                                    :key="i"
                                    class="w-5 h-5 text-yellow-400" 
                                    fill="currentColor" 
                                    viewBox="0 0 20 20"
                                >
                                    <path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z" />
                                </svg>
                            </div>
                            <span 
                                class="px-3 py-1 text-xs font-semibold rounded-full border"
                                :class="getStatusColor(testimonial.status)"
                            >
                                {{ getStatusLabel(testimonial.status) }}
                            </span>
                        </div>

                        <p class="text-gray-700 dark:text-gray-300 mb-4">
                            "{{ testimonial.content }}"
                        </p>

                        <!-- Rejection reason -->
                        <div 
                            v-if="testimonial.status === 'rejected' && testimonial.rejection_reason"
                            class="bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg p-3 mb-4"
                        >
                            <p class="text-sm text-red-700 dark:text-red-400">
                                <strong>Motivo del rechazo:</strong> {{ testimonial.rejection_reason }}
                            </p>
                        </div>

                        <div class="flex items-center justify-between text-sm text-gray-500 dark:text-gray-400">
                            <span>Enviado: {{ testimonial.submitted_at }}</span>
                            <div class="flex items-center gap-4">
                                <span v-if="testimonial.reviewed_at">
                                    Revisado: {{ testimonial.reviewed_at }}
                                </span>
                                <button
                                    v-if="testimonial.status === 'rejected'"
                                    @click="deleteTestimonial(testimonial.id)"
                                    class="text-red-600 hover:text-red-700 font-medium"
                                >
                                    Eliminar
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Empty state -->
                <div 
                    v-else-if="canSubmit" 
                    class="text-center py-12 text-gray-500 dark:text-gray-400"
                >
                    <svg class="w-16 h-16 mx-auto mb-4 text-gray-300 dark:text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
                    </svg>
                    <p>Aún no has enviado ningún testimonio.</p>
                    <p class="text-sm mt-1">¡Comparte tu experiencia con otros coaches!</p>
                </div>
            </div>
        </div>
    </CoachLayout>
</template>
