"use client";

import dynamic from "next/dynamic";
import { useTranslation } from "react-i18next";
import Input from "@/components/ui/Input";
import CheckBox from "@/components/ui/CheckBox";
import type { VolunteerFormValues } from "./volunteerFormTypes";
const LocationMapPicker = dynamic(() => import("@/components/ui/LocationMapPicker"), { ssr: false });
interface VolunteerLocationFieldsProps {
  values: VolunteerFormValues;
  setFieldValue: (field: string, value: unknown) => void;
  setFieldTouched: (field: string, touched: boolean) => void;
  selectedLanguage: string;
  /** Map picker vs. a pasted link — one or the other, never both. */
  showMapPicker: boolean;
  onUseMapPicker: () => void;
  onUseLocationLink: () => void;
  /** Suppresses a geocode round-trip when the map itself set the address. */
  skipNextGeocodeRef: { current: boolean };
}

/** A maps URL is the required answer. The optional pin is only for distance checks. */
export default function VolunteerLocationFields({ values, setFieldValue, setFieldTouched }: VolunteerLocationFieldsProps) {
  const { t } = useTranslation();
  return <div className="mb-4">
    <Input name="locationUrl" label={t("COMMON.LOCATION_URL")} dir="ltr" onFocus={() => setFieldTouched("locationUrl", true)} />
    <CheckBox id="requiresAttendanceLocation" label={t("COMMON.ATTENDANCE_LOCATION_TOGGLE")} checked={values.requiresAttendanceLocation} onChange={checked => setFieldValue("requiresAttendanceLocation", checked)} />
    <p className="mt-1 text-sm text-secondary-102">{t("COMMON.ATTENDANCE_LOCATION_TOGGLE_HINT", { meters: 500 })}</p>
    {values.requiresAttendanceLocation && <LocationMapPicker latitude={values.latitude} longitude={values.longitude} onPick={(lat, lng) => {
      setFieldValue("latitude", lat); setFieldValue("longitude", lng);
      if (!values.locationUrl) setFieldValue("locationUrl", `https://maps.google.com/?q=${lat},${lng}`);
    }} />}
  </div>;
}
