<script setup>
/**
 * Public Blog Post View
 */
import { Head, Link, usePage } from '@inertiajs/vue3';
import { ref, computed } from 'vue';

const props = defineProps({
    post: Object,
    relatedPosts: Array,
    systemLogo: String,
    siteName: String,
});

const $page = usePage();
const user = computed(() => $page.props.auth?.user);
const userMenuOpen = ref(false);
const mobileMenuOpen = ref(false);

const formatDate = (date) => {
    return new Date(date).toLocaleDateString('es-ES', {
        day: 'numeric',
        month: 'long',
        year: 'numeric',
    });
};

const shareUrl = typeof window !== 'undefined' ? window.location.href : '';
const shareText = props.post?.title;

const shareLinks = {
    twitter: `https://twitter.com/intent/tweet?url=${encodeURIComponent(shareUrl)}&text=${encodeURIComponent(shareText)}`,
    facebook: `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(shareUrl)}`,
    linkedin: `https://www.linkedin.com/sharing/share-offsite/?url=${encodeURIComponent(shareUrl)}`,
    whatsapp: `https://wa.me/?text=${encodeURIComponent(shareText + ' ' + shareUrl)}`,
};

// Fitness colors
const colors = {
    primary: '#39FF14',
    secondary: '#FF6B35',
    accent: '#00D4FF',
};

// Navigation links
const navLinks = [
    { href: '/#features', label: 'Características' },
    { href: '/#plans', label: 'Planes' },
    { href: '/#testimonials', label: 'Testimonios' },
    { href: '/contacto', label: 'Contacto' },
];
</script>

<template>
    <Head>
        <title>{{ post.meta_title || post.title }}</title>
        <meta name="description" :content="post.meta_description || post.excerpt" />
        <meta property="og:title" :content="post.title" />
        <meta property="og:description" :content="post.excerpt" />
        <meta property="og:image" :content="post.featured_image ? `/storage/${post.featured_image}` : ''" />
        <meta property="og:type" content="article" />
        <meta name="twitter:card" content="summary_large_image" />
    </Head>

    <div class="min-h-screen bg-[#0A0A0A]">
        <!-- Navigation -->
        <nav class="fixed top-0 left-0 right-0 z-50 bg-[#0A0A0A]/95 backdrop-blur-md border-b border-gray-800">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
                <div class="flex justify-between items-center h-16 md:h-20">
                    <!-- Logo -->
                    <Link href="/" class="flex items-center gap-3">
                        <img 
                            v-if="systemLogo" 
                            :src="systemLogo" 
                            :alt="siteName" 
                            class="h-10 md:h-12 w-auto logo-white"
                        />
                        <template v-else>
                            <div class="w-10 h-10 md:w-12 md:h-12 rounded-xl flex items-center justify-center" 
                                 :style="{ background: `linear-gradient(135deg, ${colors.primary}, ${colors.secondary})` }">
                                <svg class="w-6 h-6 md:w-7 md:h-7 text-black" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M3 6h2m0 0v12m0-12h2m14 0h2m-2 0v12m0-12h-2M7 6v12m10-12v12M7 12h10" />
                                </svg>
                            </div>
                            <span class="text-xl md:text-2xl font-black text-white">{{ siteName }}</span>
                        </template>
                    </Link>

                    <!-- Desktop Navigation -->
                    <div class="hidden md:flex items-center gap-8">
                        <a v-for="link in navLinks" 
                           :key="link.href"
                           :href="link.href"
                           class="text-gray-400 hover:text-white font-medium transition-colors duration-200">
                            {{ link.label }}
                        </a>
                        <Link href="/blog" class="text-white font-medium">Blog</Link>
                    </div>
                    
                    <!-- CTA Buttons / User Menu -->
                    <div class="hidden md:flex items-center gap-4">
                        <template v-if="!user">
                            <Link :href="route('login')" class="text-gray-300 hover:text-white font-semibold transition-colors">
                                Iniciar Sesión
                            </Link>
                            <Link :href="route('register')" 
                                  class="px-6 py-2.5 rounded-full font-bold text-black transition-all hover:scale-105"
                                  :style="{ background: `linear-gradient(135deg, ${colors.primary}, ${colors.accent})` }">
                                Comenzar Gratis
                            </Link>
                        </template>
                        <template v-else>
                            <div class="relative">
                                <button 
                                    @click="userMenuOpen = !userMenuOpen"
                                    class="flex items-center gap-3 px-4 py-2 rounded-full bg-gray-800/50 border border-gray-700 hover:border-gray-600"
                                >
                                    <div class="w-8 h-8 rounded-full overflow-hidden"
                                         :style="{ background: `linear-gradient(135deg, ${colors.primary}, ${colors.secondary})` }">
                                        <img v-if="user.avatar_url" :src="user.avatar_url" class="w-full h-full object-cover" />
                                        <span v-else class="w-full h-full flex items-center justify-center text-black font-bold text-sm">
                                            {{ user.name?.charAt(0).toUpperCase() }}
                                        </span>
                                    </div>
                                    <span class="text-white font-medium max-w-[120px] truncate">{{ user.name }}</span>
                                </button>
                                <div v-show="userMenuOpen" class="absolute right-0 mt-2 w-56 bg-gray-900 border border-gray-800 rounded-xl shadow-xl z-50">
                                    <div class="p-4 border-b border-gray-800">
                                        <p class="text-white font-medium truncate">{{ user.name }}</p>
                                        <p class="text-sm text-gray-400 truncate">{{ user.email }}</p>
                                    </div>
                                    <div class="py-2">
                                        <Link :href="route('dashboard')" class="block px-4 py-2.5 text-gray-300 hover:text-white hover:bg-gray-800/50">Mi Panel</Link>
                                        <Link :href="route('profile.edit')" class="block px-4 py-2.5 text-gray-300 hover:text-white hover:bg-gray-800/50">Mi Perfil</Link>
                                    </div>
                                    <div class="py-2 border-t border-gray-800">
                                        <Link :href="route('logout')" method="post" as="button" class="block w-full text-left px-4 py-2.5 text-red-400 hover:bg-red-900/20">Cerrar Sesión</Link>
                                    </div>
                                </div>
                            </div>
                        </template>
                    </div>

                    <!-- Mobile Menu Button -->
                    <button @click="mobileMenuOpen = !mobileMenuOpen" class="md:hidden p-2 text-gray-400 hover:text-white">
                        <svg v-if="!mobileMenuOpen" class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
                        </svg>
                        <svg v-else class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                        </svg>
                    </button>
                </div>

                <!-- Mobile Menu -->
                <div v-show="mobileMenuOpen" class="md:hidden py-4 border-t border-gray-800">
                    <div class="flex flex-col gap-4">
                        <a v-for="link in navLinks" :key="link.href" :href="link.href" @click="mobileMenuOpen = false"
                           class="text-gray-400 hover:text-white font-medium py-2">{{ link.label }}</a>
                        <Link href="/blog" class="text-white font-medium py-2">Blog</Link>
                        <div v-if="!user" class="flex flex-col gap-3 pt-4 border-t border-gray-800">
                            <Link :href="route('login')" class="text-center py-3 text-gray-300 font-semibold">Iniciar Sesión</Link>
                            <Link :href="route('register')" class="text-center py-3 rounded-full font-bold text-black"
                                  :style="{ background: `linear-gradient(135deg, ${colors.primary}, ${colors.accent})` }">Comenzar Gratis</Link>
                        </div>
                        <div v-else class="pt-4 border-t border-gray-800">
                            <Link :href="route('dashboard')" class="block py-2 text-gray-300">Mi Panel</Link>
                            <Link :href="route('logout')" method="post" as="button" class="block w-full text-left py-2 text-red-400">Cerrar Sesión</Link>
                        </div>
                    </div>
                </div>
            </div>
        </nav>

        <!-- Article Header -->
        <header class="pt-20 md:pt-24 bg-gray-900 border-b border-gray-800">
            <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
                <!-- Breadcrumb -->
                <nav class="flex items-center gap-2 text-sm text-gray-400 mb-6">
                    <Link :href="route('blog.index')" class="hover:text-[#39FF14]">Blog</Link>
                    <span>/</span>
                    <Link 
                        v-if="post.category"
                        :href="route('blog.category', post.category.slug)"
                        class="hover:text-[#39FF14]"
                    >
                        {{ post.category.name }}
                    </Link>
                </nav>

                <!-- Title -->
                <h1 class="text-3xl md:text-4xl lg:text-5xl font-bold text-white leading-tight mb-6">
                    {{ post.title }}
                </h1>

                <!-- Meta -->
                <div class="flex flex-wrap items-center gap-4 text-sm text-gray-400">
                    <div class="flex items-center gap-2">
                        <div class="w-10 h-10 rounded-full flex items-center justify-center text-black font-medium"
                             :style="{ background: `linear-gradient(135deg, ${colors.primary}, ${colors.secondary})` }">
                            {{ post.author?.name?.charAt(0) || 'A' }}
                        </div>
                        <div>
                            <p class="font-medium text-white">{{ post.author?.name }}</p>
                            <p class="text-xs">{{ formatDate(post.published_at) }}</p>
                        </div>
                    </div>
                    <span class="hidden sm:inline">•</span>
                    <span>{{ post.reading_time || Math.ceil((post.content?.length || 0) / 1000) }} min lectura</span>
                    <span class="hidden sm:inline">•</span>
                    <span>{{ post.views?.toLocaleString() || 0 }} vistas</span>
                </div>
            </div>
        </header>

        <!-- Featured Image -->
        <div v-if="post.featured_image" class="bg-gray-900">
            <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
                <img 
                    :src="`/storage/${post.featured_image}`"
                    :alt="post.title"
                    class="w-full h-auto max-h-[500px] object-cover rounded-2xl"
                />
            </div>
        </div>

        <!-- Content -->
        <article class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
            <div class="bg-gray-900 border border-gray-800 rounded-2xl p-8 lg:p-12">
                <!-- Content -->
                <div 
                    class="prose prose-lg prose-invert max-w-none prose-headings:font-bold prose-a:text-[#39FF14] prose-img:rounded-xl"
                    v-html="post.content"
                ></div>

                <!-- Tags -->
                <div v-if="post.tags?.length" class="mt-12 pt-8 border-t border-gray-800">
                    <h3 class="text-sm font-medium text-gray-400 mb-3">Etiquetas</h3>
                    <div class="flex flex-wrap gap-2">
                        <Link 
                            v-for="tag in post.tags" 
                            :key="tag.id"
                            :href="route('blog.tag', tag.slug)"
                            class="px-3 py-1 bg-gray-800 text-gray-300 text-sm rounded-full hover:bg-[#39FF14]/20 hover:text-[#39FF14] transition-colors"
                        >
                            #{{ tag.name }}
                        </Link>
                    </div>
                </div>

                <!-- Share -->
                <div class="mt-8 pt-8 border-t border-gray-800">
                    <h3 class="text-sm font-medium text-gray-400 mb-3">Compartir</h3>
                    <div class="flex gap-3">
                        <a 
                            :href="shareLinks.twitter" 
                            target="_blank"
                            class="w-10 h-10 flex items-center justify-center bg-gray-800 text-gray-400 rounded-full hover:bg-[#1DA1F2] hover:text-white transition-colors"
                            title="Compartir en Twitter"
                        >
                            <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 24 24"><path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"/></svg>
                        </a>
                        <a 
                            :href="shareLinks.facebook" 
                            target="_blank"
                            class="w-10 h-10 flex items-center justify-center bg-gray-800 text-gray-400 rounded-full hover:bg-[#1877F2] hover:text-white transition-colors"
                            title="Compartir en Facebook"
                        >
                            <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 24 24"><path d="M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z"/></svg>
                        </a>
                        <a 
                            :href="shareLinks.linkedin" 
                            target="_blank"
                            class="w-10 h-10 flex items-center justify-center bg-gray-800 text-gray-400 rounded-full hover:bg-[#0A66C2] hover:text-white transition-colors"
                            title="Compartir en LinkedIn"
                        >
                            <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 24 24"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433c-1.144 0-2.063-.926-2.063-2.065 0-1.138.92-2.063 2.063-2.063 1.14 0 2.064.925 2.064 2.063 0 1.139-.925 2.065-2.064 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z"/></svg>
                        </a>
                        <a 
                            :href="shareLinks.whatsapp" 
                            target="_blank"
                            class="w-10 h-10 flex items-center justify-center bg-gray-800 text-gray-400 rounded-full hover:bg-[#25D366] hover:text-white transition-colors"
                            title="Compartir en WhatsApp"
                        >
                            <svg class="w-5 h-5" fill="currentColor" viewBox="0 0 24 24"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z"/></svg>
                        </a>
                    </div>
                </div>
            </div>
        </article>

        <!-- Related Posts -->
        <section v-if="relatedPosts?.length" class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
            <h2 class="text-2xl font-bold text-white mb-8">Artículos Relacionados</h2>
            
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                <article 
                    v-for="related in relatedPosts" 
                    :key="related.id"
                    class="bg-gray-900 border border-gray-800 rounded-2xl overflow-hidden hover:border-gray-700 transition-all group"
                >
                    <Link :href="route('blog.show', related.slug)">
                        <div class="aspect-video overflow-hidden">
                            <img 
                                v-if="related.featured_image"
                                :src="`/storage/${related.featured_image}`"
                                :alt="related.title"
                                class="w-full h-full object-cover group-hover:scale-105 transition-transform duration-300"
                            />
                            <div v-else class="w-full h-full bg-gradient-to-br from-[#39FF14]/20 to-[#00D4FF]/20"></div>
                        </div>
                        <div class="p-6">
                            <h3 class="font-bold text-white group-hover:text-[#39FF14] transition-colors line-clamp-2">
                                {{ related.title }}
                            </h3>
                            <p class="text-sm text-gray-500 mt-2">
                                {{ formatDate(related.published_at) }}
                            </p>
                        </div>
                    </Link>
                </article>
            </div>
        </section>

        <!-- Back to Blog -->
        <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 pb-12">
            <Link 
                :href="route('blog.index')"
                class="inline-flex items-center gap-2 text-[#39FF14] hover:text-[#00D4FF] font-medium transition-colors"
            >
                <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="M15 19l-7-7 7-7" />
                </svg>
                Volver al blog
            </Link>
        </div>

        <!-- Footer -->
        <footer class="bg-gray-900 border-t border-gray-800">
            <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
                <div class="flex flex-col md:flex-row items-center justify-between gap-6">
                    <Link href="/" class="flex items-center gap-3">
                        <img v-if="systemLogo" :src="systemLogo" :alt="siteName" class="h-8 w-auto logo-white" />
                        <span class="text-xl font-bold text-white">{{ siteName }}</span>
                    </Link>
                    <div class="flex items-center gap-6 text-gray-400 text-sm">
                        <Link href="/about" class="hover:text-white transition-colors">Sobre Nosotros</Link>
                        <Link href="/terms" class="hover:text-white transition-colors">Términos</Link>
                        <Link href="/privacy" class="hover:text-white transition-colors">Privacidad</Link>
                        <Link href="/contacto" class="hover:text-white transition-colors">Contacto</Link>
                    </div>
                </div>
                <div class="mt-8 pt-8 border-t border-gray-800 text-center text-gray-500 text-sm">
                    © {{ new Date().getFullYear() }} {{ siteName }}. Todos los derechos reservados.
                </div>
            </div>
        </footer>
    </div>
</template>

<style scoped>
.line-clamp-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Logo blanco para fondos oscuros */
.logo-white {
    filter: brightness(0) invert(1);
    transition: filter 0.3s ease;
}

.logo-white:hover {
    filter: brightness(0) invert(1) drop-shadow(0 0 8px rgba(57, 255, 20, 0.5));
}
</style>
