"use client";

import { NextIntlClientProvider } from "next-intl";
import { ReactNode, useMemo } from "react";
import { useLanguage } from "../hooks/useLanguage";

// Import all locale messages
import esCommon from "../../locales/es/common.json";
import enCommon from "../../locales/en/common.json";
import caCommon from "../../locales/ca/common.json";
import esNavigation from "../../locales/es/navigation.json";
import enNavigation from "../../locales/en/navigation.json";
import caNavigation from "../../locales/ca/navigation.json";
import esExchange from "../../locales/es/exchange.json";
import enExchange from "../../locales/en/exchange.json";
import caExchange from "../../locales/ca/exchange.json";
import esAuth from "../../locales/es/auth.json";
import enAuth from "../../locales/en/auth.json";
import caAuth from "../../locales/ca/auth.json";
import esTrades from "../../locales/es/trades.json";
import enTrades from "../../locales/en/trades.json";
import caTrades from "../../locales/ca/trades.json";
import esInvites from "../../locales/es/invites.json";
import enInvites from "../../locales/en/invites.json";
import caInvites from "../../locales/ca/invites.json";
import esProfile from "../../locales/es/profile.json";
import enProfile from "../../locales/en/profile.json";
import caProfile from "../../locales/ca/profile.json";
import esAdmin from "../../locales/es/admin.json";
import enAdmin from "../../locales/en/admin.json";
import caAdmin from "../../locales/ca/admin.json";

const messages = {
  es: {
    common: esCommon,
    navigation: esNavigation,
    exchange: esExchange,
    auth: esAuth,
    trades: esTrades,
    invites: esInvites,
    profile: esProfile,
    admin: esAdmin,
  },
  en: {
    common: enCommon,
    navigation: enNavigation,
    exchange: enExchange,
    auth: enAuth,
    trades: enTrades,
    invites: enInvites,
    profile: enProfile,
    admin: enAdmin,
  },
  ca: {
    common: caCommon,
    navigation: caNavigation,
    exchange: caExchange,
    auth: caAuth,
    trades: caTrades,
    invites: caInvites,
    profile: caProfile,
    admin: caAdmin,
  },
};

interface IntlProviderProps {
  children: ReactNode;
}

export function IntlProvider({ children }: IntlProviderProps) {
  const { locale } = useLanguage();

  const localeMessages = useMemo(() => {
    return messages[locale] || messages.es;
  }, [locale]);

  return (
    <NextIntlClientProvider locale={locale} messages={localeMessages} timeZone="Europe/Madrid">
      {children}
    </NextIntlClientProvider>
  );
}
