"use client";

import i18n from "i18next";
import { initReactI18next } from "react-i18next";

import {
  adminHtmlLang,
  applyAdminUiLocale,
  hydrateAdminUiLocale,
  type AdminApiLocale,
} from "@/lib/admin-locale";
import enAudit from "@/i18n/locales/en/audit.json";
import enAdminUsers from "@/i18n/locales/en/adminUsers.json";
import enAuth from "@/i18n/locales/en/auth.json";
import enCommon from "@/i18n/locales/en/common.json";
import enConfig from "@/i18n/locales/en/config.json";
import enDashboard from "@/i18n/locales/en/dashboard.json";
import enDraws from "@/i18n/locales/en/draws.json";
import enJackpot from "@/i18n/locales/en/jackpot.json";
import enRisk from "@/i18n/locales/en/risk.json";
import enSettlement from "@/i18n/locales/en/settlement.json";
import enPlayers from "@/i18n/locales/en/players.json";
import enTickets from "@/i18n/locales/en/tickets.json";
import enReconcile from "@/i18n/locales/en/reconcile.json";
import enReports from "@/i18n/locales/en/reports.json";
import enWallet from "@/i18n/locales/en/wallet.json";
import neAudit from "@/i18n/locales/ne/audit.json";
import neAdminUsers from "@/i18n/locales/ne/adminUsers.json";
import neAuth from "@/i18n/locales/ne/auth.json";
import neCommon from "@/i18n/locales/ne/common.json";
import neConfig from "@/i18n/locales/ne/config.json";
import neDashboard from "@/i18n/locales/ne/dashboard.json";
import neDraws from "@/i18n/locales/ne/draws.json";
import neJackpot from "@/i18n/locales/ne/jackpot.json";
import neRisk from "@/i18n/locales/ne/risk.json";
import neSettlement from "@/i18n/locales/ne/settlement.json";
import nePlayers from "@/i18n/locales/ne/players.json";
import neTickets from "@/i18n/locales/ne/tickets.json";
import neReconcile from "@/i18n/locales/ne/reconcile.json";
import neReports from "@/i18n/locales/ne/reports.json";
import neWallet from "@/i18n/locales/ne/wallet.json";
import zhAudit from "@/i18n/locales/zh/audit.json";
import zhAdminUsers from "@/i18n/locales/zh/adminUsers.json";
import zhAuth from "@/i18n/locales/zh/auth.json";
import zhCommon from "@/i18n/locales/zh/common.json";
import zhConfig from "@/i18n/locales/zh/config.json";
import zhDashboard from "@/i18n/locales/zh/dashboard.json";
import zhDraws from "@/i18n/locales/zh/draws.json";
import zhJackpot from "@/i18n/locales/zh/jackpot.json";
import zhRisk from "@/i18n/locales/zh/risk.json";
import zhSettlement from "@/i18n/locales/zh/settlement.json";
import zhPlayers from "@/i18n/locales/zh/players.json";
import zhTickets from "@/i18n/locales/zh/tickets.json";
import zhReconcile from "@/i18n/locales/zh/reconcile.json";
import zhReports from "@/i18n/locales/zh/reports.json";
import zhWallet from "@/i18n/locales/zh/wallet.json";

export const ADMIN_SUPPORTED_LANGUAGES = ["en", "ne", "zh"] as const;
export type AdminLanguage = (typeof ADMIN_SUPPORTED_LANGUAGES)[number];
export const ADMIN_DEFAULT_LANGUAGE: AdminLanguage = "zh";

const namespaces = ["common", "auth", "dashboard", "audit", "draws", "settlement", "risk", "jackpot", "players", "tickets", "reconcile", "reports", "wallet", "adminUsers", "config"] as const;

const resources = {
  en: {
    common: enCommon,
    config: enConfig,
    adminUsers: enAdminUsers,
    auth: enAuth,
    dashboard: enDashboard,
    draws: enDraws,
    jackpot: enJackpot,
    players: enPlayers,
    tickets: enTickets,
    reconcile: enReconcile,
    reports: enReports,
    risk: enRisk,
    audit: enAudit,
    settlement: enSettlement,
    wallet: enWallet,
  },
  ne: {
    common: neCommon,
    config: neConfig,
    adminUsers: neAdminUsers,
    auth: neAuth,
    dashboard: neDashboard,
    draws: neDraws,
    jackpot: neJackpot,
    players: nePlayers,
    tickets: neTickets,
    reconcile: neReconcile,
    reports: neReports,
    risk: neRisk,
    audit: neAudit,
    settlement: neSettlement,
    wallet: neWallet,
  },
  zh: {
    common: zhCommon,
    config: zhConfig,
    adminUsers: zhAdminUsers,
    auth: zhAuth,
    dashboard: zhDashboard,
    draws: zhDraws,
    jackpot: zhJackpot,
    players: zhPlayers,
    tickets: zhTickets,
    reconcile: zhReconcile,
    reports: zhReports,
    risk: zhRisk,
    audit: zhAudit,
    settlement: zhSettlement,
    wallet: zhWallet,
  },
} satisfies Record<AdminLanguage, Record<(typeof namespaces)[number], Record<string, unknown>>>;

export function normalizeAdminLanguage(lang: string | undefined): AdminLanguage {
  const base = lang?.split("-")[0]?.toLowerCase();
  if (base === "ne") return "ne";
  if (base === "zh") return "zh";
  return "en";
}

function getInitialAdminLanguage(): AdminLanguage {
  // Keep SSR and first client render aligned to avoid hydration mismatch.
  // Stored locale is applied after mount in `Providers`.
  return ADMIN_DEFAULT_LANGUAGE;
}

function syncAdminLanguage(lang: AdminLanguage): void {
  if (typeof document !== "undefined") {
    document.documentElement.lang = adminHtmlLang(lang);
  }
  applyAdminUiLocale(lang as AdminApiLocale);
}

if (!i18n.isInitialized) {
  if (typeof window !== "undefined") {
    hydrateAdminUiLocale();
  }

  const initialLanguage = getInitialAdminLanguage();

  void i18n
    .use(initReactI18next)
    .init({
      resources,
      lng: initialLanguage,
      fallbackLng: ADMIN_DEFAULT_LANGUAGE,
      supportedLngs: [...ADMIN_SUPPORTED_LANGUAGES],
      defaultNS: "common",
      ns: [...namespaces],
      load: "languageOnly",
      interpolation: {
        escapeValue: false,
      },
      react: {
        useSuspense: false,
      },
    })
    .then(() => {
      const lang = getInitialAdminLanguage();
      if (normalizeAdminLanguage(i18n.language) !== lang) {
        void i18n.changeLanguage(lang);
      }
      syncAdminLanguage(normalizeAdminLanguage(i18n.resolvedLanguage ?? i18n.language));
    });

  syncAdminLanguage(initialLanguage);
  i18n.on("languageChanged", (lang) => {
    syncAdminLanguage(normalizeAdminLanguage(lang));
  });
}

export default i18n;
