<template>
  <div class="min-h-screen bg-gray-50">
    <!-- Sticky Navbar -->
    <nav class="bg-white shadow-sm sticky top-0 z-50 border-b border-gray-100">
      <div class="max-w-7xl mx-auto px-4 sm:px-6">
        <div class="flex justify-between items-center h-16">
          <router-link to="/" class="flex items-center gap-2 group">
            <span class="text-3xl">🍔</span>
            <span class="text-xl font-bold bg-gradient-to-r from-orange-600 to-red-600 bg-clip-text text-transparent">FoodHub</span>
          </router-link>
          
          <div class="hidden md:flex items-center gap-6">
            <router-link to="/restaurants" class="text-gray-700 hover:text-orange-600 font-medium transition">Restaurants</router-link>
            <router-link v-if="isAuthenticated" to="/orders" class="text-gray-700 hover:text-orange-600 font-medium transition">Orders</router-link>
            <router-link v-if="isAuthenticated" to="/cart" class="relative">
              <span class="text-2xl">🛒</span>
              <span v-if="cartCount > 0" class="absolute -top-1 -right-1 bg-red-500 text-white text-xs w-5 h-5 rounded-full flex items-center justify-center font-bold">{{ cartCount }}</span>
            </router-link>
            <router-link v-if="!isAuthenticated" to="/login" class="text-gray-700 hover:text-orange-600 font-medium transition">Login</router-link>
            <router-link v-if="!isAuthenticated" to="/register" class="bg-gradient-to-r from-orange-600 to-red-600 text-white px-6 py-2.5 rounded-full font-semibold hover:shadow-lg transition">Sign Up</router-link>
            <button v-if="isAuthenticated" @click="logout" class="text-gray-700 hover:text-red-600 font-medium transition">Logout</button>
          </div>

          <button @click="mobileMenuOpen = !mobileMenuOpen" class="md:hidden text-gray-700">
            <svg 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>
          </button>
        </div>

        <div v-if="mobileMenuOpen" class="md:hidden py-4 space-y-3 border-t">
          <router-link to="/restaurants" @click="mobileMenuOpen = false" class="block text-gray-700 hover:text-orange-600 font-medium">Restaurants</router-link>
          <router-link v-if="isAuthenticated" to="/orders" @click="mobileMenuOpen = false" class="block text-gray-700 hover:text-orange-600 font-medium">Orders</router-link>
          <router-link v-if="!isAuthenticated" to="/login" @click="mobileMenuOpen = false" class="block text-gray-700 hover:text-orange-600 font-medium">Login</router-link>
          <router-link v-if="!isAuthenticated" to="/register" @click="mobileMenuOpen = false" class="block bg-gradient-to-r from-orange-600 to-red-600 text-white px-6 py-2.5 rounded-full font-semibold text-center">Sign Up</router-link>
        </div>
      </div>
    </nav>

    <!-- Hero Section -->
    <section class="relative bg-gradient-to-br from-orange-50 via-white to-red-50 overflow-hidden">
      <div class="absolute inset-0 opacity-10">
        <div class="absolute top-10 left-10 w-72 h-72 bg-orange-400 rounded-full blur-3xl"></div>
        <div class="absolute bottom-10 right-10 w-96 h-96 bg-red-400 rounded-full blur-3xl"></div>
      </div>
      <div class="max-w-7xl mx-auto px-4 sm:px-6 py-16 md:py-24 relative">
        <div class="text-center max-w-3xl mx-auto">
          <div class="inline-block mb-4 px-4 py-2 bg-orange-100 rounded-full">
            <span class="text-orange-600 font-semibold text-sm">🎉 Free delivery on orders over Rs 1000</span>
          </div>
          <h1 class="text-4xl md:text-6xl font-extrabold text-gray-900 mb-6 leading-tight">
            Delicious Food<br/>
            <span class="bg-gradient-to-r from-orange-600 to-red-600 bg-clip-text text-transparent">Delivered Fast</span>
          </h1>
          <p class="text-lg md:text-xl text-gray-600 mb-8">Order from your favorite restaurants and get fresh, hot meals delivered to your doorstep in minutes</p>
          <div class="flex flex-col sm:flex-row gap-4 justify-center">
            <router-link to="/restaurants" class="inline-flex items-center justify-center gap-2 bg-gradient-to-r from-orange-600 to-red-600 text-white px-8 py-4 rounded-full text-lg font-semibold hover:shadow-xl transform hover:scale-105 transition">
              <span>🍕</span>
              <span>Browse Restaurants</span>
              <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="M9 5l7 7-7 7"/>
              </svg>
            </router-link>
            <button class="inline-flex items-center justify-center gap-2 bg-white border-2 border-orange-600 text-orange-600 px-8 py-4 rounded-full text-lg font-semibold hover:bg-orange-50 transition">
              <span>📱</span>
              <span>Download App</span>
            </button>
          </div>
        </div>
      </div>
    </section>

    <!-- Features -->
    <section class="py-16 md:py-20 bg-white">
      <div class="max-w-7xl mx-auto px-4 sm:px-6">
        <div class="text-center mb-12">
          <h2 class="text-3xl md:text-4xl font-bold mb-4">Why Choose Us?</h2>
          <p class="text-gray-600 text-lg">Experience the best food delivery service</p>
        </div>
        <div class="grid grid-cols-1 md:grid-cols-3 gap-8">
          <div class="group text-center p-8 rounded-2xl hover:bg-gradient-to-br hover:from-orange-50 hover:to-red-50 transition-all duration-300">
            <div class="w-20 h-20 bg-gradient-to-br from-orange-500 to-red-500 rounded-2xl flex items-center justify-center mx-auto mb-6 text-4xl shadow-lg group-hover:scale-110 transition-transform">⚡</div>
            <h3 class="text-2xl font-bold mb-3">Fast Delivery</h3>
            <p class="text-gray-600 leading-relaxed">Get your food in 30 minutes or less with our lightning-fast delivery service</p>
          </div>
          <div class="group text-center p-8 rounded-2xl hover:bg-gradient-to-br hover:from-orange-50 hover:to-red-50 transition-all duration-300">
            <div class="w-20 h-20 bg-gradient-to-br from-orange-500 to-red-500 rounded-2xl flex items-center justify-center mx-auto mb-6 text-4xl shadow-lg group-hover:scale-110 transition-transform">🏪</div>
            <h3 class="text-2xl font-bold mb-3">Wide Selection</h3>
            <p class="text-gray-600 leading-relaxed">Hundreds of restaurants and thousands of dishes to choose from</p>
          </div>
          <div class="group text-center p-8 rounded-2xl hover:bg-gradient-to-br hover:from-orange-50 hover:to-red-50 transition-all duration-300">
            <div class="w-20 h-20 bg-gradient-to-br from-orange-500 to-red-500 rounded-2xl flex items-center justify-center mx-auto mb-6 text-4xl shadow-lg group-hover:scale-110 transition-transform">💳</div>
            <h3 class="text-2xl font-bold mb-3">Easy Payment</h3>
            <p class="text-gray-600 leading-relaxed">Multiple secure payment options including cash and online payments</p>
          </div>
        </div>
      </div>
    </section>

    <!-- Categories -->
    <section class="py-16 md:py-20 bg-gray-50">
      <div class="max-w-7xl mx-auto px-4 sm:px-6">
        <div class="text-center mb-12">
          <h2 class="text-3xl md:text-4xl font-bold mb-4">Popular Categories</h2>
          <p class="text-gray-600 text-lg">Explore your favorite cuisines</p>
        </div>
        
        <div v-if="loading" class="flex justify-center py-12">
          <div class="animate-spin rounded-full h-12 w-12 border-4 border-orange-500 border-t-transparent"></div>
        </div>
        
        <div v-else-if="categories.length === 0" class="text-center py-12">
          <p class="text-gray-500">No categories available</p>
        </div>
        
        <div v-else class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-4 md:gap-6">
          <div v-for="cat in categories" :key="cat.id" @click="$router.push('/restaurants')" class="group bg-white p-6 md:p-8 rounded-2xl shadow-sm hover:shadow-xl cursor-pointer text-center transform hover:scale-105 transition-all duration-300">
            <div class="text-5xl md:text-6xl mb-4 group-hover:scale-110 transition-transform">{{ cat.icon }}</div>
            <p class="font-bold text-gray-800 text-lg">{{ cat.name }}</p>
            <p class="text-sm text-gray-500 mt-1">{{ cat.count }}+ dishes</p>
          </div>
        </div>
      </div>
    </section>

    <!-- CTA -->
    <section class="py-16 md:py-20 bg-gradient-to-r from-orange-600 to-red-600">
      <div class="max-w-4xl mx-auto px-4 sm:px-6 text-center">
        <h2 class="text-3xl md:text-5xl font-bold text-white mb-4">Ready to Order?</h2>
        <p class="text-lg md:text-xl text-orange-100 mb-8">Join thousands of happy customers today</p>
        <router-link to="/restaurants" class="inline-block bg-white text-orange-600 px-8 py-4 rounded-full text-lg font-semibold hover:shadow-2xl transform hover:scale-105 transition">
          Get Started
        </router-link>
      </div>
    </section>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-12">
      <div class="max-w-7xl mx-auto px-4 sm:px-6">
        <div class="grid grid-cols-2 md:grid-cols-4 gap-8 mb-8">
          <div class="col-span-2 md:col-span-1">
            <h4 class="text-xl font-bold mb-3 flex items-center gap-2">
              <span>🍔</span>
              <span class="bg-gradient-to-r from-orange-400 to-red-400 bg-clip-text text-transparent">FoodHub</span>
            </h4>
            <p class="text-gray-400 text-sm">Delivering happiness, one meal at a time</p>
          </div>
          <div>
            <h5 class="font-semibold mb-3">Company</h5>
            <ul class="space-y-2 text-sm text-gray-400">
              <li><a href="#" class="hover:text-orange-400 transition">About Us</a></li>
              <li><a href="#" class="hover:text-orange-400 transition">Careers</a></li>
              <li><a href="#" class="hover:text-orange-400 transition">Contact</a></li>
            </ul>
          </div>
          <div>
            <h5 class="font-semibold mb-3">Support</h5>
            <ul class="space-y-2 text-sm text-gray-400">
              <li><a href="#" class="hover:text-orange-400 transition">Help Center</a></li>
              <li><a href="#" class="hover:text-orange-400 transition">Terms</a></li>
              <li><a href="#" class="hover:text-orange-400 transition">Privacy</a></li>
            </ul>
          </div>
          <div>
            <h5 class="font-semibold mb-3">Follow Us</h5>
            <div class="flex gap-3 text-2xl">
              <a href="#" class="hover:text-orange-400 transition">📘</a>
              <a href="#" class="hover:text-orange-400 transition">📷</a>
              <a href="#" class="hover:text-orange-400 transition">🐦</a>
            </div>
          </div>
        </div>
        <div class="border-t border-gray-800 pt-6 text-center text-sm text-gray-400">
          <p>&copy; 2024 FoodHub. All rights reserved.</p>
        </div>
      </div>
    </footer>
  </div>
</template>

<script setup>
import { computed, ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useAuthStore } from '../stores/auth';
import api from '../services/api';

const router = useRouter();
const authStore = useAuthStore();
const isAuthenticated = computed(() => authStore.isAuthenticated);
const mobileMenuOpen = ref(false);
const cartCount = ref(0);
const loading = ref(true);

const categories = ref([]);

const loadCategories = async () => {
  try {
    loading.value = true;
    const response = await api.get('/categories');
    const dbCategories = response.data.data || response.data || [];
    
    // Map database categories to display format
    const categoryIcons = {
      'Pizza': '🍕',
      'Burger': '🍔',
      'Burgers': '🍔',
      'Sushi': '🍣',
      'Dessert': '🍰',
      'Desserts': '🍰',
      'Drink': '🥤',
      'Drinks': '🥤',
      'Beverages': '🥤',
      'Salad': '🥗',
      'Salads': '🥗',
      'Asian': '🍜',
      'Chinese': '🍜',
      'Noodles': '🍜',
      'Italian': '🍝',
      'Pasta': '🍝',
      'Rice': '🍚',
      'Fried Rice': '🍚',
      'Seafood': '🦞',
      'Chicken': '🍗',
      'Breakfast': '🍳',
      'Lunch': '🍱',
      'Dinner': '🍽️',
    };
    
    categories.value = dbCategories.slice(0, 8).map(cat => ({
      id: cat.id,
      name: cat.name,
      icon: categoryIcons[cat.name] || '🍽️',
      count: cat.products_count || 0
    }));
  } catch (error) {
    console.error('Error loading categories:', error);
    // Fallback categories
    categories.value = [
      { name: 'Pizza', icon: '🍕', count: 150 },
      { name: 'Burgers', icon: '🍔', count: 120 },
      { name: 'Sushi', icon: '🍣', count: 80 },
      { name: 'Desserts', icon: '🍰', count: 200 },
      { name: 'Drinks', icon: '🥤', count: 100 },
      { name: 'Salads', icon: '🥗', count: 60 },
      { name: 'Asian', icon: '🍜', count: 180 },
      { name: 'Italian', icon: '🍝', count: 90 },
    ];
  } finally {
    loading.value = false;
  }
};

const logout = () => {
  authStore.logout();
  router.push('/');
};

onMounted(() => {
  loadCategories();
});
</script>
