// src/hooks/useProfileAddress.ts
import { useState, useEffect, useCallback } from 'react';
import toast from 'react-hot-toast';
import type { Address, UserProfile } from '../types';
import { useApi } from './useApi';
import * as apiClient from '../services/apiClient';
import { logger } from '../services/logger.client';
import { useDebounce } from './useDebounce';

const geocodeWrapper = (address: string, signal?: AbortSignal) =>
  apiClient.geocodeAddress(address, { signal });
const fetchAddressWrapper = (id: number, signal?: AbortSignal) =>
  apiClient.getUserAddress(id, { signal });

/**
 * Helper to generate a consistent address string for geocoding.
 */
const getAddressString = (address: Partial<Address>): string => {
  return [
    address.address_line_1,
    address.city,
    address.province_state,
    address.postal_code,
    address.country,
  ]
    .filter(Boolean)
    .join(', ');
};

/**
 * A custom hook to manage a user's profile address, including fetching,
 * updating, and automatic/manual geocoding.
 * @param userProfile The user's profile object.
 * @param isOpen Whether the parent component (e.g., a modal) is open. This is used to reset state.
 * @returns An object with address state and handler functions.
 */
export const useProfileAddress = (userProfile: UserProfile | null, isOpen: boolean) => {
  const [address, setAddress] = useState<Partial<Address>>({});
  const [initialAddress, setInitialAddress] = useState<Partial<Address>>({});

  const { execute: geocode, loading: isGeocoding } = useApi<{ lat: number; lng: number }, [string]>(
    geocodeWrapper,
  );
  const { execute: fetchAddress } = useApi<Address, [number]>(fetchAddressWrapper);

  // Effect to fetch or reset address based on profile and modal state
  useEffect(() => {
    const loadAddress = async () => {
      if (userProfile?.address_id) {
        logger.debug(
          `[useProfileAddress] Profile has address_id: ${userProfile.address_id}. Fetching.`,
        );
        const fetchedAddress = await fetchAddress(userProfile.address_id);
        if (fetchedAddress) {
          logger.debug('[useProfileAddress] Successfully fetched address:', fetchedAddress);
          setAddress(fetchedAddress);
          setInitialAddress(fetchedAddress);
        } else {
          logger.warn(
            `[useProfileAddress] Fetch returned null for addressId: ${userProfile.address_id}.`,
          );
          setAddress({});
          setInitialAddress({});
        }
      } else {
        logger.debug('[useProfileAddress] Profile has no address_id. Resetting address form.');
        setAddress({});
        setInitialAddress({});
      }
    };

    if (isOpen && userProfile) {
      loadAddress();
    } else {
      logger.debug(
        '[useProfileAddress] Modal is closed or profile is null. Resetting address state.',
      );
      setAddress({});
      setInitialAddress({});
    }
  }, [isOpen, userProfile, fetchAddress]); // fetchAddress is stable from useApi

  const handleAddressChange = useCallback((field: keyof Address, value: string) => {
    setAddress((prev) => ({ ...prev, [field]: value }));
  }, []);

  const handleManualGeocode = useCallback(async () => {
    const addressString = getAddressString(address);

    if (!addressString) {
      toast.error('Please fill in the address fields before geocoding.');
      return;
    }

    logger.debug(`[useProfileAddress] Manual geocode triggering for: ${addressString}`);
    const result = await geocode(addressString);
    if (result) {
      const { lat, lng } = result;
      setAddress((prev) => ({ ...prev, latitude: lat, longitude: lng }));
      toast.success('Address re-geocoded successfully!');
    }
  }, [address, geocode]);

  // --- Automatic Geocoding Logic ---
  const debouncedAddress = useDebounce(address, 1500);

  useEffect(() => {
    const handleAutoGeocode = async () => {
      logger.debug('[useProfileAddress] Auto-geocode effect triggered by debouncedAddress change');

      if (JSON.stringify(debouncedAddress) === JSON.stringify(initialAddress)) {
        logger.debug(
          '[useProfileAddress] Skipping auto-geocode: address is unchanged from initial load.',
        );
        return;
      }

      const addressString = getAddressString(debouncedAddress);

      // Don't geocode an empty address or if we already have coordinates.
      if (!addressString || (debouncedAddress.latitude && debouncedAddress.longitude)) {
        logger.debug(
          '[useProfileAddress] Skipping auto-geocode: empty string or coordinates already exist',
          { hasString: !!addressString, hasCoords: !!debouncedAddress.latitude },
        );
        return;
      }

      logger.debug(`[useProfileAddress] Auto-geocoding: "${addressString}"`);
      const result = await geocode(addressString);
      if (result) {
        logger.debug('[useProfileAddress] Auto-geocode API returned result:', result);
        const { lat, lng } = result;
        setAddress((prev) => ({ ...prev, latitude: lat, longitude: lng }));
        toast.success('Address geocoded successfully!');
      }
    };

    handleAutoGeocode();
  }, [debouncedAddress, initialAddress, geocode]);

  return {
    address,
    initialAddress,
    isGeocoding,
    handleAddressChange,
    handleManualGeocode,
  };
};
