<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { useFavoritesStore } from '@/stores/favorites'
import { API_URL } from '@/services/apiClient'

const { t } = useI18n()
const router = useRouter()
const favoritesStore = useFavoritesStore()
const selectedFilter = ref('all')


onMounted(async () => {
    await favoritesStore.loadFavorites()
})

function getImageUrl(path?: string) {
    if (!path) return `https://ui-avatars.com/api/?name=Favorite&background=fee715&color=101820`
    if (path.startsWith('http')) return path
    return `${API_URL}${path.startsWith('/') ? '' : '/'}${path}`
}

async function removeFavorite(event: Event, itemType: string, itemId: string) {
    event.stopPropagation()
    if (confirm(t('favorites.removeConfirm'))) {
        await favoritesStore.removeFavorite(itemType, itemId)
    }
}

const navigateToItem = (item: any) => {
    if (item.item_type === 'route') {
        router.push('/schedules')
    } else if (item.item_type === 'taxi') {
        router.push('/taxi')
    } else if (item.item_type === 'business') {
        router.push('/business/' + item.item_id)
    }
}

</script>

<template>
    <div class="favorites-view">
        <!-- Hero Header -->
        <header class="favorites-header">
            <div class="header-overlay"></div>
            <div class="header-content">
                <div class="header-icon">
                    <span class="material-icons">stars</span>
                </div>
                <h1>{{ t('favorites.title') }}</h1>
                <p class="subtitle">{{ t('favorites.subtitle') }}</p>
            </div>
        </header>

        <!-- Category Filter (igual a Descubrir) -->
        <div class="filters-container-wrapper">
            <div class="filters-card">
                <div class="filter-group">
                    <label>
                        <span class="material-icons">filter_list</span>
                        Filtrar favoritos
                    </label>
                    <div class="select-wrapper">
                        <select v-model="selectedFilter" class="filter-select">
                            <option value="all">Todas las categorías</option>
                            <option v-if="favoritesStore.routes.length > 0" value="routes">Rutas ({{ favoritesStore.routes.length }})</option>
                            <option v-if="favoritesStore.taxis.length > 0" value="taxis">Taxis ({{ favoritesStore.taxis.length }})</option>
                            <option v-if="favoritesStore.businesses.length > 0" value="businesses">Negocios ({{ favoritesStore.businesses.length }})</option>
                            <option v-if="favoritesStore.coupons.length > 0" value="coupons">Eventos ({{ favoritesStore.coupons.length }})</option>
                        </select>
                        <span class="material-icons dropdown-icon">expand_more</span>
                    </div>
                </div>
            </div>
        </div>

        <div v-if="favoritesStore.isLoading" class="loading">
            <div class="spinner"></div>
            <p>{{ t('common.loading') }}</p>
        </div>

        <div v-else class="dashboard-area">
            <!-- Global Empty State -->
            <div v-if="favoritesStore.favorites.length === 0" class="empty-state">
                <div class="empty-icon">
                    <span class="material-icons">favorite_border</span>
                </div>
                <h3>{{ t('favorites.empty.subtitle') }}</h3>
            </div>

            <div v-else class="sections-list">
                <!-- ROUTES SECTION -->
                <section v-if="(selectedFilter === 'all' || selectedFilter === 'routes') && favoritesStore.routes.length > 0" class="fav-section">
                    <div class="section-header">
                        <span class="material-icons">directions_bus</span>
                        <h2>{{ t('favorites.tabs.routes') }}</h2>
                    </div>
                    <div class="compact-list">
                        <div v-for="item in favoritesStore.routes" :key="item.id" class="list-item" @click="navigateToItem(item)">
                            <div class="item-visual bg-yellow">
                                <span class="material-icons">directions_bus</span>
                            </div>
                            <div class="item-details">
                                <h3>{{ item.item_name }}</h3>
                                <p>{{ t('favorites.viewSchedules') }}</p>
                            </div>
                            <button class="item-remove" @click.stop="removeFavorite($event, item.item_type, item.item_id)">
                                <span class="material-icons">close</span>
                            </button>
                        </div>
                    </div>
                </section>

                <!-- TAXIS SECTION -->
                <section v-if="(selectedFilter === 'all' || selectedFilter === 'taxis') && favoritesStore.taxis.length > 0" class="fav-section">
                    <div class="section-header">
                        <span class="material-icons">local_taxi</span>
                        <h2>{{ t('favorites.tabs.taxis') }}</h2>
                    </div>
                    <div class="compact-list">
                        <div v-for="item in favoritesStore.taxis" :key="item.id" class="list-item" @click="navigateToItem(item)">
                            <div class="item-visual thumb">
                                <img :src="getImageUrl(item.item_image)" :alt="item.item_name">
                            </div>
                            <div class="item-details">
                                <h3>{{ item.item_name }}</h3>
                                <p>{{ t('favorites.contact') }}</p>
                            </div>
                            <button class="item-remove" @click.stop="removeFavorite($event, item.item_type, item.item_id)">
                                <span class="material-icons">close</span>
                            </button>
                        </div>
                    </div>
                </section>

                <!-- BUSINESSES SECTION -->
                <section v-if="(selectedFilter === 'all' || selectedFilter === 'businesses') && favoritesStore.businesses.length > 0" class="fav-section">
                    <div class="section-header">
                        <span class="material-icons">store</span>
                        <h2>{{ t('favorites.tabs.businesses') }}</h2>
                    </div>
                    <div class="horizontal-scroll">
                        <div v-for="item in favoritesStore.businesses" :key="item.id" class="fav-card-mini" @click="navigateToItem(item)">
                            <div class="mini-card-img">
                                <img :src="getImageUrl(item.item_image)" :alt="item.item_name">
                                <button class="mini-remove" @click.stop="removeFavorite($event, item.item_type, item.item_id)">
                                    <span class="material-icons">close</span>
                                </button>
                            </div>
                            <div class="mini-card-body">
                                <h3>{{ item.item_name }}</h3>
                                <p>{{ t('favorites.viewDetails') }}</p>
                            </div>
                        </div>
                    </div>
                </section>

                <!-- EVENTS SECTION -->
                <section v-if="(selectedFilter === 'all' || selectedFilter === 'coupons') && favoritesStore.coupons.length > 0" class="fav-section">
                    <div class="section-header">
                        <span class="material-icons">event</span>
                        <h2>{{ t('favorites.tabs.coupons') }}</h2>
                    </div>
                    <div class="compact-list">
                        <div v-for="item in favoritesStore.coupons" :key="item.id" class="list-item">
                            <div class="item-visual thumb">
                                <img :src="getImageUrl(item.item_image)" :alt="item.item_name">
                            </div>
                            <div class="item-details">
                                <h3>{{ item.item_name }}</h3>
                                <p>{{ t('favorites.saved') }}</p>
                            </div>
                            <button class="item-remove" @click.stop="removeFavorite($event, item.item_type, item.item_id)">
                                <span class="material-icons">close</span>
                            </button>
                        </div>
                    </div>
                </section>
            </div>
        </div>
    </div>
</template>

<style scoped>
.favorites-view {
    min-height: 100vh;
    background: var(--bg-primary);
    padding-bottom: 80px;
    box-sizing: border-box;
}

/* Hero Header */
.favorites-header {
  position: relative;
  background: linear-gradient(135deg, #fee715 0%, #facc15 100%);
  color: #101820;
  padding: 40px 20px 60px;
  text-align: center;
  overflow: hidden;
}

.header-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: 
    radial-gradient(circle at 20% 50%, rgba(255,255,255,0.2) 0%, transparent 50%),
    radial-gradient(circle at 80% 80%, rgba(255,255,255,0.2) 0%, transparent 50%);
  pointer-events: none;
}

.header-content {
  position: relative;
  z-index: 1;
  max-width: 600px;
  margin: 0 auto;
}

.header-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
  background: rgba(16, 24, 32, 0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(10px);
  animation: float 3s ease-in-out infinite;
}

.header-icon .material-icons {
  font-size: 40px;
  color: #101820;
}

@keyframes float {
  0%, 100% { transform: translateY(0px); }
  50% { transform: translateY(-10px); }
}

.header-content h1 {
  font-size: 2.5rem;
  font-weight: 900;
  margin-bottom: 12px;
  color: #101820;
}

/* Filter Card Area */
.filters-container-wrapper {
  max-width: 1200px;
  margin: 30px auto 30px;
  padding: 0 20px;
  position: relative;
  z-index: 10;
}

.filters-card {
    background: var(--card-bg);
    border-radius: 20px;
    padding: 20px;
    box-shadow: 0 20px 50px rgba(0,0,0,0.3);
    border: 1px solid var(--active-color);
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.filter-group label {
  font-weight: 700;
  color: var(--text-primary);
  font-size: 0.95rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.filter-group label .material-icons {
  font-size: 20px;
  color: var(--active-color);
}

.select-wrapper {
  position: relative;
}

.filter-select {
  width: 100%;
  padding: 14px 44px 14px 16px;
  border-radius: 14px;
  border: 2px solid var(--border-color);
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 1rem;
  font-weight: 700;
  appearance: none;
  cursor: pointer;
  transition: all 0.3s;
}

.filter-select option {
  background: var(--card-bg);
  color: var(--text-primary);
  padding: 10px;
}

.filter-select:focus {
  outline: none;
  border-color: var(--active-color);
  box-shadow: 0 0 0 4px rgba(254, 231, 21, 0.1);
}

.dropdown-icon {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-secondary);
  pointer-events: none;
}

/* Dashboard Sections */
.sections-list {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
    display: flex;
    flex-direction: column;
    gap: 48px;
}

.fav-section {
    scroll-margin-top: 100px;
}

.section-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 24px;
    color: var(--text-primary);
}

.section-header .material-icons {
    color: var(--active-color);
    font-size: 28px;
}

.section-header h2 {
    font-size: 1.8rem;
    font-weight: 900;
    margin: 0;
}

/* Compact List Style */
.compact-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.list-item {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    padding: 16px;
    display: flex;
    align-items: center;
    gap: 16px;
    transition: all 0.3s ease;
    cursor: pointer;
}

.list-item:hover {
    transform: translateX(10px);
    border-color: var(--active-color);
    background: var(--hover-bg);
}

.item-visual {
    width: 60px;
    height: 60px;
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    overflow: hidden;
}

.item-visual.bg-yellow {
    background: var(--active-color);
    color: #101820;
}

.item-visual.thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.item-details {
    flex: 1;
}

.item-details h3 {
    margin: 0;
    font-size: 1.15rem;
    font-weight: 800;
}

.item-details p {
    margin: 4px 0 0;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.item-remove {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    width: 36px;
    height: 36px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
}

.item-remove:hover {
    background: #ff4757;
    color: white;
    border-color: #ff4757;
}

/* Horizontal Scroll for Businesses */
.horizontal-scroll {
    display: flex;
    gap: 16px;
    overflow-x: auto;
    padding: 4px 4px 20px;
    scrollbar-width: none;
}

.horizontal-scroll::-webkit-scrollbar {
    display: none;
}

.fav-card-mini {
    width: 240px;
    flex-shrink: 0;
    background: var(--card-bg);
    border-radius: 24px;
    border: 1px solid var(--border-color);
    overflow: hidden;
    transition: all 0.3s ease;
    cursor: pointer;
}

.fav-card-mini:hover {
    transform: translateY(-8px);
    border-color: var(--active-color);
}

.mini-card-img {
    height: 140px;
    position: relative;
    overflow: hidden;
}

.mini-card-img img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.mini-remove {
    position: absolute;
    top: 8px;
    right: 8px;
    background: rgba(0,0,0,0.5);
    color: white;
    border: none;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(4px);
    cursor: pointer;
}

.mini-card-body {
    padding: 16px;
}

.mini-card-body h3 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 800;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.mini-card-body p {
    margin: 4px 0 0;
    font-size: 0.85rem;
    color: var(--active-color);
    font-weight: 700;
}

/* States */
.empty-state {
  text-align: center;
  padding: 40px 24px;
  background: var(--header-bg);
  border-radius: 30px;
  border: 2px dashed var(--border-color);
  max-width: 500px;
  margin: 0 auto;
}

.empty-icon {
  width: 100px;
  height: 100px;
  margin: 0 auto 24px;
  background: var(--bg-secondary);
  border-radius: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--active-color);
  opacity: 0.5;
}

.empty-state h3 {
  font-size: 1.5rem;
  font-weight: 900;
  margin-bottom: 12px;
}

.cta-link {
    display: inline-flex;
    padding: 16px 32px;
    background: var(--active-color);
    color: #101820;
    text-decoration: none;
    border-radius: 16px;
    font-weight: 900;
    margin-top: 24px;
}

@media (max-width: 768px) {
    .favorites-header {
      padding: 40px 16px 60px;
    }
    
    .header-content h1 {
      font-size: 2.1rem;
    }

    .sections-list {
        padding: 0 16px;
        gap: 32px;
    }

    .section-header h2 {
        font-size: 1.5rem;
    }

    .list-item {
        padding: 12px;
    }

    .item-visual {
        width: 50px;
        height: 50px;
    }

    .item-details h3 {
        font-size: 1rem;
    }

    .fav-card-mini {
        width: 200px;
    }
}
</style>
