import Link from "next/link";
import { locationSlugs, type LocationSlug } from "@alterra/shared";
import MapBase from "../MapBase";
import { labelPosition, zonePins } from "@/lib/map";

/** Map view of the listing: one pin per zone sized by the number of matching properties. */
export default function ListingMap({
  counts,
  names,
  hrefFor,
  active,
  medellin,
  airport,
  note,
  hint,
}: {
  counts: Partial<Record<LocationSlug, number>>;
  names: Record<LocationSlug, string>;
  hrefFor: (slug: LocationSlug) => string;
  active?: LocationSlug;
  medellin: string;
  airport: string;
  note: string;
  hint: string;
}) {
  return (
    <figure className="relative border border-ink/10 bg-cream">
      <svg viewBox="0 0 760 400" className="block h-auto w-full" aria-labelledby="listing-map-title">
        <title id="listing-map-title">{hint}</title>
        <MapBase medellin={medellin} airport={airport} />
        {locationSlugs.map((slug) => {
          const pin = zonePins[slug];
          const count = counts[slug] ?? 0;
          const isActive = active === slug;
          const label = labelPosition(pin, 26);
          return (
            <Link key={slug} href={hrefFor(slug)} className="group">
              <circle cx={pin.x} cy={pin.y} r={count ? 22 : 12} fill={count ? "var(--color-forest)" : "var(--color-stone)"} fillOpacity={count ? 1 : 0.35} stroke={isActive ? "var(--color-gold)" : "var(--color-cream)"} strokeWidth={isActive ? 4 : 2} />
              {count > 0 && (
                <text x={pin.x} y={pin.y + 6} textAnchor="middle" fontSize="17" fill="var(--color-cream)" className="font-serif">
                  {count}
                </text>
              )}
              <text x={label.x} y={label.y} textAnchor={label.anchor} fontSize="20" className="font-serif" fill={isActive ? "var(--color-forest)" : "var(--color-ink)"} fontWeight={isActive ? 600 : 400}>
                {names[slug]}
              </text>
            </Link>
          );
        })}
      </svg>
      <figcaption className="flex flex-wrap justify-between gap-2 border-t border-ink/10 px-4 py-3 text-xs text-stone">
        <span>{hint}</span>
        <span className="uppercase tracking-[0.2em]">{note}</span>
      </figcaption>
    </figure>
  );
}
