import {
  createContext,
  useCallback,
  useContext,
  useEffect,
  useMemo,
  useState,
  type ReactNode,
} from "react";
import { formatMoney, formatPlanPrice } from "../lib/i18n/format";
import { translate } from "../lib/i18n/messages";
import { loadUiPrefs, saveUiPrefs } from "../lib/i18n/prefs";
import type { AppCurrency, AppLocale } from "../lib/i18n/types";

type I18nContextValue = {
  locale: AppLocale;
  currency: AppCurrency;
  setLocale: (locale: AppLocale) => void;
  setCurrency: (currency: AppCurrency) => void;
  setPrefs: (prefs: { locale?: AppLocale; currency?: AppCurrency }) => void;
  t: (key: string, params?: Record<string, string | number>) => string;
  formatMoney: (amountTwd: number) => string;
  formatPlanPrice: (amountTwd: number) => string;
};

const I18nContext = createContext<I18nContextValue | null>(null);

export function I18nProvider({ children }: { children: ReactNode }) {
  const [locale, setLocaleState] = useState<AppLocale>(() => loadUiPrefs().locale);
  const [currency, setCurrencyState] = useState<AppCurrency>(
    () => loadUiPrefs().currency,
  );

  useEffect(() => {
    document.documentElement.lang = locale === "en" ? "en" : "zh-Hant";
  }, [locale]);

  const setLocale = useCallback((next: AppLocale) => {
    setLocaleState(next);
    saveUiPrefs({ locale: next });
  }, []);

  const setCurrency = useCallback((next: AppCurrency) => {
    setCurrencyState(next);
    saveUiPrefs({ currency: next });
  }, []);

  const setPrefs = useCallback(
    (prefs: { locale?: AppLocale; currency?: AppCurrency }) => {
      const nextLocale = prefs.locale ?? locale;
      const nextCurrency = prefs.currency ?? currency;
      setLocaleState(nextLocale);
      setCurrencyState(nextCurrency);
      saveUiPrefs({ locale: nextLocale, currency: nextCurrency });
    },
    [locale, currency],
  );

  const t = useCallback(
    (key: string, params?: Record<string, string | number>) =>
      translate(locale, key, params),
    [locale],
  );

  const value = useMemo<I18nContextValue>(
    () => ({
      locale,
      currency,
      setLocale,
      setCurrency,
      setPrefs,
      t,
      formatMoney: (amountTwd: number) => formatMoney(amountTwd, currency, locale),
      formatPlanPrice: (amountTwd: number) =>
        formatPlanPrice(amountTwd, currency, locale),
    }),
    [locale, currency, setLocale, setCurrency, setPrefs, t],
  );

  return <I18nContext.Provider value={value}>{children}</I18nContext.Provider>;
}

export function useI18n(): I18nContextValue {
  const ctx = useContext(I18nContext);
  if (!ctx) throw new Error("useI18n must be used within I18nProvider");
  return ctx;
}
