import { formatUsd, formatHa } from './format'
import { primaryCommodity, modalityLabel, stageCode } from './listing-view'
import type { ListingWithRelations } from './listings'

export type MapPin = {
  id: string
  slug: string
  title: string
  lat: number
  lng: number
  mineral: string
  priceLabel: string
  haLabel: string
  region: string
  city: string
  verified: boolean
  stageCode: string
  modality: string
}

/** Activos con coordenadas → pines serializables para el mapa cliente. */
export function toMapPins(listings: ListingWithRelations[]): MapPin[] {
  return listings
    .filter((l) => l.lat != null && l.lng != null)
    .map((l) => ({
      id: l.id,
      slug: l.slug,
      title: l.title,
      lat: l.lat as number,
      lng: l.lng as number,
      mineral: primaryCommodity(l) ?? 'OTRO',
      priceLabel: formatUsd(l.priceUsd),
      haLabel: formatHa(l.surfaceHa),
      region: l.region ?? '',
      city: l.city ?? '',
      verified: l.verified,
      stageCode: stageCode(l.stage),
      modality: modalityLabel(l.dealTypes),
    }))
}
