<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useRouteStore } from '@/stores/route'
import { analyticsService } from '@/services/analyticsService'
import FavoriteButton from '@/components/FavoriteButton.vue'

const router = useRouter()
const routeStore = useRouteStore()

const originSearch = ref('')
const destinationSearch = ref('')

onMounted(async () => {
  analyticsService.logEvent({ event_name: 'screen_view', screen_name: 'Routes' })
  await routeStore.loadRoutes()
})

const handleSearch = async () => {
  await routeStore.loadRoutes({
    originCity: originSearch.value,
    destinationCity: destinationSearch.value
  })
}

const goToSchedules = (route: any) => {
  analyticsService.logEvent({ 
    event_name: 'route_selected', 
    item_id: route.name,
    properties: { route_id: route.id }
  })
  routeStore.selectRoute(route.id, route.name)
  router.push('/schedules')
}
</script>

<template>
  <div class="routes-view">
    <div class="header">
      <h1>Búsqueda de Rutas</h1>
      <p>Encuentra tu próximo viaje fácilmente</p>
    </div>

    <div class="search-box">
      <div class="input-group">
        <span class="material-icons">location_on</span>
        <input 
          v-model="originSearch" 
          placeholder="Origen (Ciudad)"
          @keyup.enter="handleSearch"
        >
      </div>
      <div class="divider">
        <span class="material-icons">swap_vert</span>
      </div>
      <div class="input-group">
        <span class="material-icons">flag</span>
        <input 
          v-model="destinationSearch" 
          placeholder="Destino (Ciudad)"
          @keyup.enter="handleSearch"
        >
      </div>
      <button @click="handleSearch" class="search-btn">
        <span class="material-icons">search</span>
        Buscar Rutas
      </button>
    </div>

    <div v-if="routeStore.isLoadingRoutes" class="loading">
      <div class="spinner"></div>
      <p>Buscando mejores rutas...</p>
    </div>

    <div v-else-if="routeStore.allRoutes.length === 0" class="no-results">
        <span class="material-icons">sentiment_dissatisfied</span>
        <p>No encontramos rutas que coincidan con tu búsqueda.</p>
        <button @click="originSearch = ''; destinationSearch = ''; handleSearch()" class="reset-btn">Ver todas las rutas</button>
    </div>

    <div v-else class="routes-list">
      <div 
        v-for="route in routeStore.allRoutes" 
        :key="route.id" 
        class="route-card"
        @click="goToSchedules(route)"
      >
        <div class="route-header">
          <div class="route-name">
            <span class="dot" :style="{ backgroundColor: route.color || '#fee715' }"></span>
            <h3>{{ route.name }}</h3>
          </div>
          <div class="route-actions">
            <FavoriteButton 
              item-type="route" 
              :item-id="route.id" 
              :item-name="route.name"
            />
            <span class="material-icons chevron">chevron_right</span>
          </div>
        </div>
        <div class="route-details">
          <div class="city-flow">
            <span>{{ route.origin_city }}</span>
            <span class="material-icons">arrow_forward</span>
            <span>{{ route.destination_city }}</span>
          </div>
          <p v-if="route.distance_km" class="meta">
            {{ route.distance_km }} km • {{ route.estimated_duration_minutes }} min aprox.
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.routes-view {
  padding: 24px;
  background: var(--bg-primary);
  min-height: 100vh;
}

.header {
  margin-bottom: 32px;
}

.header h1 {
  font-size: 28px;
  font-weight: 800;
  color: var(--text-primary);
  margin-bottom: 4px;
}

.header p {
  color: var(--text-secondary);
  font-size: 16px;
}

.search-box {
  background: var(--card-bg);
  border-radius: 24px;
  padding: 24px;
  box-shadow: 0 10px 30px var(--shadow);
  margin-bottom: 32px;
}

.input-group {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-secondary);
  padding: 12px 20px;
  border-radius: 16px;
}

.input-group input {
  border: none;
  background: transparent;
  width: 100%;
  font-size: 16px;
  font-weight: 500;
  outline: none;
}

.divider {
  display: flex;
  justify-content: center;
  margin: 8px 0;
  color: var(--text-secondary);
}

.search-btn {
  width: 100%;
  margin-top: 20px;
  background: var(--header-bg);
  color: var(--header-text);
  border: none;
  padding: 16px;
  border-radius: 16px;
  font-weight: 800;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  transition: transform 0.2s;
}

.search-btn:active {
  transform: scale(0.98);
}

.routes-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.route-card {
  background: var(--card-bg);
  border-radius: 20px;
  padding: 20px;
  cursor: pointer;
  box-shadow: 0 4px 15px var(--shadow);
  transition: transform 0.2s, box-shadow 0.2s;
}

.route-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 10px 25px rgba(0,0,0,0.08);
}

.route-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.route-name {
  display: flex;
  align-items: center;
  gap: 12px;
}

.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
}

.route-name h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
}

.city-flow {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 4px;
}

.route-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.chevron {
  color: #ced4da;
}

.loading, .no-results {
  text-align: center;
  padding: 60px 20px;
  color: #666;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #fee715;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.reset-btn {
  margin-top: 16px;
  background: transparent;
  border: 1px solid #dee2e6;
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
}
</style>
