import { useCallback } from 'react';
import { useLocale } from './useLocale';
import {
  convertWeight,
  convertHeight,
  convertTemperature,
  convertVolume,
} from '@/lib/utils/unitConversion';

/**
 * Custom hook for formatting dates, times, numbers, and measurements
 * based on the user's locale and measurement preferences
 */
export function useFormatting() {
  const { language, measurementSystem } = useLocale();

  const formatDate = useCallback(
    (date: Date | string, options?: Intl.DateTimeFormatOptions) => {
      const dateObj = typeof date === 'string' ? new Date(date) : date;
      return new Intl.DateTimeFormat(language, options || {
        year: 'numeric',
        month: 'long',
        day: 'numeric',
      }).format(dateObj);
    },
    [language]
  );

  const formatTime = useCallback(
    (date: Date | string, options?: Intl.DateTimeFormatOptions) => {
      const dateObj = typeof date === 'string' ? new Date(date) : date;
      return new Intl.DateTimeFormat(language, options || {
        hour: '2-digit',
        minute: '2-digit',
      }).format(dateObj);
    },
    [language]
  );

  const formatDateTime = useCallback(
    (date: Date | string, options?: Intl.DateTimeFormatOptions) => {
      const dateObj = typeof date === 'string' ? new Date(date) : date;
      return new Intl.DateTimeFormat(language, options || {
        year: 'numeric',
        month: 'short',
        day: 'numeric',
        hour: '2-digit',
        minute: '2-digit',
      }).format(dateObj);
    },
    [language]
  );

  const formatNumber = useCallback(
    (value: number, options?: Intl.NumberFormatOptions) => {
      return new Intl.NumberFormat(language, options).format(value);
    },
    [language]
  );

  const formatWeight = useCallback(
    (valueInKg: number, showUnit: boolean = true) => {
      const converted = convertWeight(valueInKg, measurementSystem);
      const formatted = formatNumber(converted.value, {
        minimumFractionDigits: 0,
        maximumFractionDigits: 2,
      });
      return showUnit ? `${formatted} ${converted.unit}` : formatted;
    },
    [measurementSystem, formatNumber]
  );

  const formatHeight = useCallback(
    (valueInCm: number, showUnit: boolean = true) => {
      const converted = convertHeight(valueInCm, measurementSystem);
      const formatted = formatNumber(converted.value, {
        minimumFractionDigits: 0,
        maximumFractionDigits: 1,
      });
      return showUnit ? `${formatted} ${converted.unit}` : formatted;
    },
    [measurementSystem, formatNumber]
  );

  const formatTemperature = useCallback(
    (valueInCelsius: number, showUnit: boolean = true) => {
      const converted = convertTemperature(valueInCelsius, measurementSystem);
      const formatted = formatNumber(converted.value, {
        minimumFractionDigits: 1,
        maximumFractionDigits: 1,
      });
      return showUnit ? `${formatted}${converted.unit}` : formatted;
    },
    [measurementSystem, formatNumber]
  );

  const formatVolume = useCallback(
    (valueInMl: number, showUnit: boolean = true) => {
      const converted = convertVolume(valueInMl, measurementSystem);
      const formatted = formatNumber(converted.value, {
        minimumFractionDigits: 0,
        maximumFractionDigits: 1,
      });
      return showUnit ? `${formatted} ${converted.unit}` : formatted;
    },
    [measurementSystem, formatNumber]
  );

  const formatDuration = useCallback(
    (minutes: number) => {
      if (minutes < 60) {
        return `${minutes} min`;
      }
      const hours = Math.floor(minutes / 60);
      const mins = minutes % 60;
      return mins > 0 ? `${hours}h ${mins}min` : `${hours}h`;
    },
    []
  );

  return {
    formatDate,
    formatTime,
    formatDateTime,
    formatNumber,
    formatWeight,
    formatHeight,
    formatTemperature,
    formatVolume,
    formatDuration,
    measurementSystem,
  };
}
