import type { MapMarker } from '@/components/map/MapView';
import { formatCurrency } from '@/lib/utils';
import { resolveJobCoordinates } from '@/lib/geocode';

export function buildJobMapMarkers(
  jobs: Record<string, unknown>[],
  hrefForJob: (id: string) => string = (id) => `/jobs/${id}`
): MapMarker[] {
  const markers: MapMarker[] = [];

  for (const job of jobs) {
    const coords = resolveJobCoordinates({
      id: job.id as string,
      location: job.location as string,
      latitude: job.latitude as number | null,
      longitude: job.longitude as number | null,
    });
    if (!coords) continue;

    const category = job.category as { name: string } | undefined;
    markers.push({
      id: job.id as string,
      type: 'job',
      lat: coords.lat,
      lng: coords.lng,
      title: job.title as string,
      subtitle: job.description as string,
      category: category?.name,
      location: job.location as string,
      budget: job.budget != null ? formatCurrency(Number(job.budget)) : undefined,
      status: job.status as string,
      postedAt: job.createdAt as string,
      dateNeeded: (job.dateNeeded as string | null) ?? null,
      href: hrefForJob(job.id as string),
    });
  }

  return markers;
}
