import { getRequestConfig } from "next-intl/server";
import { cookies } from "next/headers";

import { DEFAULT_LOCALE, LOCALE_COOKIE, isLocale } from "@/shared/i18n/locale";

/**
 * next-intl 请求配置（without i18n routing 模式）。
 *
 * locale 来源优先级：
 * 1. cookie `NEXT_LOCALE`（用户主动切换时写入）
 * 2. 默认值 `zh-CN`
 *
 * 不使用 Accept-Language 自动协商，避免 SSR 与客户端 hydration 不一致。
 * 用户切换语言时通过 setLocaleAction 写入 cookie + router.refresh()。
 */
export default getRequestConfig(async () => {
  const cookieStore = await cookies();
  const cookieValue = cookieStore.get(LOCALE_COOKIE)?.value;
  const locale = isLocale(cookieValue) ? cookieValue : DEFAULT_LOCALE;

  // 按命名空间拆分加载，避免单文件过大
  const [
    common,
    auth,
    onboarding,
    classes,
    errors,
    dashboard,
    examHomework,
    announcements,
    messages,
    notifications,
    settings,
    textbooks,
    grade,
    lessonPreparation,
    grades,
    diagnostic,
    attendance,
    elective,
    school,
    ai,
    practice,
    student,
    errorBook,
    audit,
    scheduling,
    coursePlans,
    files,
    users,
    leave,
    nav,
    rbac,
    questions,
    parent,
    invitationCodes,
  ] = await Promise.all([
    import(`@/shared/i18n/messages/${locale}/common.json`),
    import(`@/shared/i18n/messages/${locale}/auth.json`),
    import(`@/shared/i18n/messages/${locale}/onboarding.json`),
    import(`@/shared/i18n/messages/${locale}/classes.json`),
    import(`@/shared/i18n/messages/${locale}/errors.json`),
    import(`@/shared/i18n/messages/${locale}/dashboard.json`),
    import(`@/shared/i18n/messages/${locale}/exam-homework.json`),
    import(`@/shared/i18n/messages/${locale}/announcements.json`),
    import(`@/shared/i18n/messages/${locale}/messages.json`),
    import(`@/shared/i18n/messages/${locale}/notifications.json`),
    import(`@/shared/i18n/messages/${locale}/settings.json`),
    import(`@/shared/i18n/messages/${locale}/textbooks.json`),
    import(`@/shared/i18n/messages/${locale}/grade.json`),
    import(`@/shared/i18n/messages/${locale}/lesson-preparation.json`),
    import(`@/shared/i18n/messages/${locale}/grades.json`),
    import(`@/shared/i18n/messages/${locale}/diagnostic.json`),
    import(`@/shared/i18n/messages/${locale}/attendance.json`),
    import(`@/shared/i18n/messages/${locale}/elective.json`),
    import(`@/shared/i18n/messages/${locale}/school.json`),
    import(`@/shared/i18n/messages/${locale}/ai.json`),
    import(`@/shared/i18n/messages/${locale}/practice.json`),
    import(`@/shared/i18n/messages/${locale}/student.json`),
    import(`@/shared/i18n/messages/${locale}/error-book.json`),
    import(`@/shared/i18n/messages/${locale}/audit.json`),
    import(`@/shared/i18n/messages/${locale}/scheduling.json`),
    import(`@/shared/i18n/messages/${locale}/course-plans.json`),
    import(`@/shared/i18n/messages/${locale}/files.json`),
    import(`@/shared/i18n/messages/${locale}/users.json`),
    import(`@/shared/i18n/messages/${locale}/leave.json`),
    import(`@/shared/i18n/messages/${locale}/nav.json`),
    import(`@/shared/i18n/messages/${locale}/rbac.json`),
    import(`@/shared/i18n/messages/${locale}/questions.json`),
    import(`@/shared/i18n/messages/${locale}/parent.json`),
    import(`@/shared/i18n/messages/${locale}/invitation-codes.json`),
  ]);

  return {
    locale,
    messages: {
      common: common.default,
      auth: auth.default,
      onboarding: onboarding.default,
      classes: classes.default,
      errors: errors.default,
      dashboard: dashboard.default,
      examHomework: examHomework.default,
      announcements: announcements.default,
      messages: messages.default,
      notifications: notifications.default,
      settings: settings.default,
      textbooks: textbooks.default,
      grade: grade.default,
      lessonPreparation: lessonPreparation.default,
      grades: grades.default,
      diagnostic: diagnostic.default,
      attendance: attendance.default,
      elective: elective.default,
      school: school.default,
      ai: ai.default,
      practice: practice.default,
      student: student.default,
      errorBook: errorBook.default,
      audit: audit.default,
      scheduling: scheduling.default,
      coursePlans: coursePlans.default,
      files: files.default,
      users: users.default,
      leave: leave.default,
      nav: nav.default,
      rbac: rbac.default,
      questions: questions.default,
      parent: parent.default,
      invitationCodes: invitationCodes.default,
    },
  };
});
