<template>
  <div class="uber-search-container" :class="{ 'compact-mode': isCompact }">
    <!-- Floating Triggers -->
    <div v-if="!showPanel" class="triggers-row">
      <!-- Shrunk Trigger (Icon only) -->
      <div 
        v-if="isRouteActive" 
        class="uber-search-trigger circular" 
        @click="$emit('open')"
        :title="t('map.search')"
      >
        <span class="material-icons">search</span>
      </div>

      <!-- Normal Trigger -->
      <div 
        v-else
        class="uber-search-trigger-compact" 
        @click="$emit('open')"
      >
        <span class="material-icons search-icon">directions_bus</span>
        <span class="trigger-label">{{ t('map.viewRoutes') }}</span>
      </div>

      <!-- Slot for additional triggers like ArrivalBanner -->
      <slot name="extra-triggers"></slot>
    </div>

    <!-- Uber-style Search Panel -->
    <Transition name="uber-slide">
      <div v-if="showPanel" class="uber-search-panel">
        <div class="uber-search-header">
          <button class="back-btn" @click="$emit('close')">
            <span class="material-icons">arrow_back</span>
          </button>
          <div class="search-title">{{ t('map.availableRoutes') }}</div>
        </div>
        <!-- Search Input -->
        <div class="search-input-wrapper">
          <span class="material-icons search-field-icon">search</span>
          <input 
            v-model="searchQuery" 
            type="text" 
            :placeholder="t('map.search')" 
            class="route-search-input"
            autofocus
          />
        </div>

        <!-- Results -->
        <div class="uber-results custom-scrollbar">
          <div 
            v-for="route in filteredRoutes" 
            :key="route.id" 
            class="uber-result-item"
            :class="{ 'selected-route': route.id === selectedRouteId && wasSelectedFromMap }"
            @click="$emit('select-route', route)"
          >
            <div class="result-icon">
              <span class="material-icons">directions_bus</span>
            </div>
            <div class="result-content">
              <div class="result-name">{{ route.name }}</div>
              <div class="result-address">{{ t('map.busRoute') }}</div>
            </div>
            <span class="material-icons check-icon">
              {{ route.id === selectedRouteId ? 'check_circle' : 'chevron_right' }}
            </span>
          </div>
        </div>
      </div>
    </Transition>
  </div>
</template>

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

const props = defineProps<{
  showPanel: boolean
  isCompact: boolean
  isRouteActive: boolean
  allRoutes: any[]
  selectedRouteId: string | null
  wasSelectedFromMap: boolean
}>()

defineEmits(['open', 'close', 'select-route'])

const { t } = useI18n()
const searchQuery = ref('')

const filteredRoutes = computed(() => {
  if (!searchQuery.value.trim()) return props.allRoutes
  const query = searchQuery.value.toLowerCase().trim()
  return props.allRoutes.filter(r => 
    r.name.toLowerCase().includes(query) || 
    (r.origin_city && r.origin_city.toLowerCase().includes(query)) ||
    (r.destination_city && r.destination_city.toLowerCase().includes(query))
  )
})
</script>

<style scoped>
.uber-search-container {
  position: fixed;
  top: 90px;
  left: 16px;
  right: 16px;
  z-index: 1100;
  pointer-events: none;
}

.uber-search-container > * {
  pointer-events: auto;
}

.uber-search-trigger {
  background: var(--header-bg);
  backdrop-filter: blur(20px);
  height: 44px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  padding: 0 16px;
  box-shadow: var(--shadow);
  cursor: pointer;
  border: 1px solid var(--border-color);
  width: 100%;
  max-width: 500px;
}

.uber-search-trigger-compact {
  background: var(--active-color) !important;
  color: #101820 !important;
  height: 44px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  cursor: pointer;
  border: none;
}

.trigger-label {
  font-size: 0.9rem;
  font-weight: 800;
  text-transform: uppercase;
}

.uber-search-trigger.circular {
  width: 44px;
  padding: 0;
  justify-content: center;
}

.triggers-row {
  display: flex;
  gap: 12px;
  align-items: center;
}

.uber-search-panel {
  background: var(--header-bg);
  backdrop-filter: blur(20px);
  border-radius: 24px;
  padding: 24px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.4);
  max-width: 500px;
  margin: 0 auto;
}

.uber-search-header {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
}

.back-btn {
  background: var(--bg-secondary);
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 20px;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.search-title {
  font-size: 1.25rem;
  font-weight: 800;
}

.search-input-wrapper {
  position: relative;
  margin-bottom: 20px;
}

.search-field-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-secondary);
  pointer-events: none;
}

.route-search-input {
  width: 100%;
  padding: 12px 12px 12px 40px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  transition: border-color 0.2s;
}

.route-search-input:focus {
  outline: none;
  border-color: var(--active-color);
  box-shadow: 0 0 0 2px rgba(254, 231, 21, 0.2);
}

.uber-results {
  max-height: 400px;
  overflow-y: auto;
}

.uber-result-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.2s;
}

.uber-result-item:hover {
  background: var(--hover-bg);
}

.selected-route {
  background: rgba(254, 231, 21, 0.1);
  border: 1px solid var(--active-color);
}

.result-icon {
  width: 48px;
  height: 48px;
  background: var(--bg-secondary);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--active-color);
}

.result-content {
  flex: 1;
}

.result-name {
  font-weight: 800;
  color: var(--text-primary);
}

.result-address {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.check-icon {
  color: var(--active-color);
}

.uber-slide-enter-active, .uber-slide-leave-active { transition: all 0.5s ease; }
.uber-slide-enter-from, .uber-slide-leave-to { transform: translateY(20px); opacity: 0; }
</style>
