<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useI18n } from 'vue-i18n'
import { useRoute } from 'vue-router'

const { t } = useI18n()
const route = useRoute()

const isMainView = computed(() => {
  return route.name === 'TaxisLocales' || route.name === 'ViajesTuristicos'
})
const mountError = ref(false)

const reloadPage = () => {
  window.location.reload()
}

onMounted(async () => {
  try {
    console.log('Transporte Hub mounted')
  } catch (e) {
    console.error('Error mounting Transporte Hub:', e)
    mountError.value = true
  }
})
</script>

<template>
  <div class="taxi-view">
    <header v-if="isMainView" class="header-main">
      <h1 class="brand-title">{{ t('taxi.title') }}</h1>
      <nav class="hub-tabs" role="tablist" :aria-label="t('taxi.title')">
        <div class="tabs-background">
          <router-link
            to="/transporte/taxis"
            class="hub-tab"
            role="tab"
            :aria-selected="!route.path.includes('viajes-turisticos')"
            active-class="active"
            exact-active-class="active"
          >
            <span class="material-icons notranslate" translate="no" aria-hidden="true">local_taxi</span>
            {{ t('taxi.tabLocal') }}
          </router-link>
          <router-link
            to="/transporte/viajes-turisticos"
            class="hub-tab"
            role="tab"
            :aria-selected="route.path.includes('viajes-turisticos')"
            active-class="active"
            :class="{ 'active': route.path.includes('viajes-turisticos') }"
          >
            <span class="material-icons notranslate" translate="no" aria-hidden="true">directions_bus</span>
            {{ t('taxi.tabIntercity') }}
          </router-link>
          <div class="tab-slider" :class="{ 'slide-right': route.path.includes('viajes-turisticos') }" aria-hidden="true"></div>
        </div>
      </nav>
    </header>

    <div v-if="mountError" class="error-container" role="alert">
      <span class="material-icons notranslate" translate="no" aria-hidden="true">error_outline</span>
      <p>{{ t('taxi.errorLoading') }}</p>
      <button @click="reloadPage" class="retry-btn">
        <span class="material-icons notranslate" translate="no" aria-hidden="true">refresh</span>
        {{ t('common.retry') }}
      </button>
    </div>

    <router-view v-else v-slot="{ Component }">
      <transition name="fade" mode="out-in">
        <keep-alive>
          <component :is="Component" />
        </keep-alive>
      </transition>
    </router-view>
  </div>
</template>

<style scoped>
.taxi-view {
  min-height: 100dvh;
  position: relative;
  padding: 0 0 150px;
}

.header-main {
  padding: 24px 16px;
  text-align: center;
}

.brand-title {
  color: var(--header-text);
  font-size: 1.5rem;
  font-weight: 800;
  margin: 0;
}

/* Transport Hub Tabs */
.hub-tabs {
  margin-top: 24px;
  display: flex;
  justify-content: center;
}

.tabs-background {
  background: var(--bg-secondary);
  padding: 4px;
  border-radius: 16px;
  display: flex;
  position: relative;
  border: 1px solid var(--border-color);
  width: 100%;
  max-width: 400px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(8px);
}

.hub-tab {
  flex: 1;
  padding: 12px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-weight: 700;
  font-size: 0.9rem;
  cursor: pointer;
  z-index: 2;
  transition: color 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-decoration: none;
  overflow: hidden;
  position: relative;
  min-height: 44px;
  border-radius: 12px;
}

.hub-tab:focus-visible {
  outline: 2px solid var(--active-color);
  outline-offset: 2px;
}

.hub-tab::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 100%;
  height: 100%;
  background: white;
  border-radius: 50%;
  opacity: 0;
  transform: translate(-50%, -50%) scale(0);
  transition: all 0.4s ease;
}

.hub-tab:active::after {
  width: 200%;
  height: 200%;
  opacity: 0.1;
  transform: translate(-50%, -50%) scale(1);
  transition: 0s;
}

.hub-tab.active {
  color: #101820 !important;
}

.hub-tab .material-icons {
  font-size: 18px;
}

.tab-slider {
  position: absolute;
  top: 4px;
  bottom: 4px;
  left: 4px;
  width: calc(50% - 6px);
  background: #FEE715;
  border-radius: 12px;
  transition: transform 0.32s cubic-bezier(0.34, 1.56, 0.64, 1);
  z-index: 1;
  box-shadow: 0 4px 20px rgba(254, 231, 21, 0.4);
  will-change: transform;
}

.tab-slider.slide-right {
  transform: translateX(calc(100% + 4px));
}

@media (prefers-reduced-motion: reduce) {
  .tab-slider {
    transition: none;
  }
  .hub-tab {
    transition: none;
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.18s ease;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

@media (prefers-reduced-motion: reduce) {
  .fade-enter-active,
  .fade-leave-active {
    transition: none;
  }
}

.error-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  text-align: center;
  color: var(--text-secondary);
}

.error-container .material-icons {
  font-size: 48px;
  color: #ef4444;
  margin-bottom: 16px;
}

.retry-btn {
  margin-top: 16px;
  padding: 10px 24px;
  background: var(--active-color);
  color: #101820;
  border: none;
  border-radius: 12px;
  font-weight: 800;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: transform 0.15s ease;
  min-height: 44px;
}

.retry-btn:active {
  transform: scale(0.95);
}

.retry-btn:focus-visible {
  outline: 2px solid var(--active-color);
  outline-offset: 2px;
}
</style>
