import { useEffect, useState } from "react";
import { LAST_POS_KEY } from "./mapConfig";
import { fetchIpLocation } from "../../api/client";

export function useUserPosition(mapRef, hasLastView) {
  const [userPosition, setUserPosition] = useState(null); // [lng, lat]

  useEffect(() => {
    const map = mapRef.current;
    if (!map) return;

    let cancelled = false;
    let hasPos = false;

    function saveUserPos(lat, lon) {
      if (cancelled) return;
      hasPos = true;
      const coords = [lon, lat];
      setUserPosition(coords);
      try {
        localStorage.setItem(LAST_POS_KEY, JSON.stringify({ lat, lon }));
      } catch {}

      if (!hasLastView) {
        map.flyTo({ center: coords, zoom: 9, speed: 1.2, curve: 1.5, essential: true });
      }
    }

    try {
      const raw = localStorage.getItem(LAST_POS_KEY);
      if (raw) {
        const p = JSON.parse(raw);
        if (typeof p.lat === "number" && typeof p.lon === "number") {
          hasPos = true;
          setUserPosition([p.lon, p.lat]);
        }
      }
    } catch {}

    async function viaIpFallback() {
      if (cancelled || hasPos) return;
      const ipCoords = await fetchIpLocation();
      if (!ipCoords || cancelled) return;
      const [lon, lat] = ipCoords;
      saveUserPos(lat, lon);
    }

    if ("geolocation" in navigator) {
      navigator.geolocation.getCurrentPosition(
        (pos) => {
          if (cancelled) return;
          saveUserPos(pos.coords.latitude, pos.coords.longitude);
        },
        () => {
          if (cancelled || hasPos) return;
          viaIpFallback();
        },
        { enableHighAccuracy: true, timeout: 8000, maximumAge: 30000 }
      );
    } else if (!hasPos) {
      viaIpFallback();
    }

    return () => {
      cancelled = true;
    };
  }, [mapRef, hasLastView]);

  return userPosition;
}
