import type { Place, POI } from '@/types/route';
import type { TripPlace } from '@/types/trip-ui';

/**
 * Adapter to convert TripPlace (used in trip-ui.ts / useTripStore)
 * to Place (used in route.ts / route-store / MapTilerMap)
 */
export const tripPlaceToPlace = (tp: TripPlace): Place => ({
  id: tp.id,
  name: tp.name,
  description: tp.description || '',
  latitude: tp.position.lat,
  longitude: tp.position.lng,
  category: (tp.type || 'attraction') as any,
  imageUrl: tp.img,
  rating: tp.rating,
  priority: tp.order_index,
});

/**
 * Adapter to convert POI/Place to TripPlace
 */
export const placeToTripPlace = (p: Place | POI, orderIndex: number): TripPlace => ({
  id: p.id,
  tripPlaceId: `new-${Date.now()}-${p.id}`,
  name: p.name,
  description: p.description,
  position: { 
    lat: 'latitude' in p ? p.latitude : (p as any).coordinates?.lat || 0, 
    lng: 'longitude' in p ? p.longitude : (p as any).coordinates?.lng || 0 
  },
  type: p.category || 'attraction',
  img: 'imageUrl' in p ? p.imageUrl : (p as any).image_url,
  rating: p.rating,
  duration: '2 hours',
  order_index: orderIndex,
});
