<template>
  <div class="map-container h-96 rounded-lg overflow-hidden border bg-gray-50">
    <!-- Simplified map representation -->
    <div class="h-full flex items-center justify-center p-4">
      <div class="w-full max-w-2xl">
        <h3 class="text-lg font-semibold text-gray-700 mb-4 text-center">Маршрут логистики</h3>
        
        <!-- Route visualization -->
        <div class="relative">
          <!-- Route line -->
          <div class="absolute top-1/2 left-0 right-0 h-1 bg-blue-500 transform -translate-y-1/2 z-0"></div>
          
          <!-- Location points -->
          <div class="relative z-10 flex justify-between items-center">
            <div 
              v-for="(point, index) in routePoints" 
              :key="point.id"
              class="flex flex-col items-center"
            >
              <!-- Point marker -->
              <div 
                class="w-4 h-4 rounded-full border-2 border-white shadow-md mb-2"
                :class="getPointColor(point.type)"
              ></div>
              
              <!-- Point info -->
              <div class="text-center bg-white rounded-lg shadow p-2 max-w-32">
                <p class="text-xs font-medium text-gray-800">{{ point.name }}</p>
                <p class="text-xs text-gray-500">{{ point.country }}</p>
                <div v-if="point.stage" class="mt-1">
                  <p class="text-xs text-blue-600">{{ point.stage.name }}</p>
                  <p v-if="point.stage.selectedCompany" class="text-xs text-gray-500">
                    {{ point.stage.selectedCompany.name }}
                  </p>
                </div>
              </div>
            </div>
          </div>
        </div>
        
        <!-- Route summary -->
        <div class="mt-6 grid grid-cols-2 gap-4 text-center">
          <div class="bg-white rounded-lg p-3 shadow">
            <div class="text-lg font-semibold text-blue-600">{{ getTotalDistance() }} км</div>
            <div class="text-xs text-gray-500">примерное расстояние</div>
          </div>
          <div class="bg-white rounded-lg p-3 shadow">
            <div class="text-lg font-semibold text-green-600">{{ getCountries() }}</div>
            <div class="text-xs text-gray-500">стран в маршруте</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
const props = defineProps({
  stages: {
    type: Array,
    default: () => []
  }
})

// Координаты точек маршрута
const locationData = {
  'Kenya Coffee Farm': { country: 'Kenya', lat: -1.2921, lng: 36.8219 },
  'Port of Mombasa': { country: 'Kenya', lat: -4.0435, lng: 39.6682 },
  'Novorossiysk Port': { country: 'Russia', lat: 44.7233, lng: 37.7683 },
  'Moscow Distribution Center': { country: 'Russia', lat: 55.7558, lng: 37.6176 }
}

const routePoints = computed(() => {
  const points = []
  
  props.stages.forEach((stage, index) => {
    if (stage.stageType === 'transport') {
      // Добавляем исходную точку
      const sourceData = locationData[stage.sourceLocationName]
      if (sourceData && !points.find(p => p.name === stage.sourceLocationName)) {
        points.push({
          id: `source-${stage.uuid}`,
          name: stage.sourceLocationName,
          country: sourceData.country,
          type: 'source',
          stage: stage
        })
      }
      
      // Добавляем целевую точку
      const destData = locationData[stage.destinationLocationName]
      if (destData && !points.find(p => p.name === stage.destinationLocationName)) {
        points.push({
          id: `dest-${stage.uuid}`,
          name: stage.destinationLocationName,
          country: destData.country,
          type: 'destination',
          stage: stage
        })
      }
    } else {
      // Service этап
      const locationData_service = locationData[stage.locationName]
      if (locationData_service && !points.find(p => p.name === stage.locationName)) {
        points.push({
          id: `service-${stage.uuid}`,
          name: stage.locationName,
          country: locationData_service.country,
          type: 'service',
          stage: stage
        })
      }
    }
  })
  
  return points
})

const getPointColor = (type) => {
  const colors = {
    source: 'bg-green-500',
    destination: 'bg-red-500',
    service: 'bg-blue-500'
  }
  return colors[type] || 'bg-gray-500'
}

const getTotalDistance = () => {
  // Примерные расстояния между точками
  return '15,000' // Кения → Россия примерно 15,000 км
}

const getCountries = () => {
  const countries = new Set()
  routePoints.value.forEach(point => {
    countries.add(point.country)
  })
  return countries.size
}
</script>

<style scoped>
.map-container {
  position: relative;
}
</style>