// i18n 国际化（P6-4）
// 依据：02-architecture-design.md §17 国际化、F4 裁决（i18n key 命名 error.<service>.<code_snake>）
// - 支持 zh-CN（默认）+ en-US
// - 错误码 key：error.<service>.<code_snake>（F4）
// - 简单翻译表（后续可迁移到 next-intl）

export type Locale = "zh-CN" | "en-US";

const DEFAULT_LOCALE: Locale = "zh-CN";

const translations: Record<Locale, Record<string, string>> = {
  "zh-CN": {
    // 通用
    "common.loading": "加载中...",
    "common.error": "加载失败",
    "common.retry": "重试",
    "common.save": "保存",
    "common.cancel": "取消",
    "common.confirm": "确认",

    // 导航
    "nav.dashboard": "仪表盘",
    "nav.grades": "成绩",
    "nav.attendance": "考勤",
    "nav.homework": "作业",
    "nav.weakness": "学情",
    "nav.notifications": "通知",
    "nav.preferences": "偏好",

    // 子女切换
    "child.switcher.empty": "未绑定子女",
    "child.switcher.select": "选择子女",

    // 错误码（F4 命名：error.<service>.<code_snake>）
    "error.iam.invalid_credentials": "邮箱或密码错误",
    "error.iam.invalid_refresh": "refresh token 无效",
    "error.iam.token_expired": "token 已过期",
    "error.parent.child_not_found": "子女不存在",
    "error.parent.permission_denied": "无权限访问",
    "error.parent.network_error": "网络错误",
    "error.parent.graphql_error": "数据加载失败",

    // 通知事件
    "notification.grade_recorded": "成绩发布",
    "notification.homework_graded": "作业评分",
    "notification.homework_assigned": "作业布置",
    "notification.exam_published": "考试发布",
    "notification.attendance_alert": "考勤提醒",
    "notification.school_announcement": "学校公告",
    "notification.teacher_message": "老师消息",
    "notification.fee_reminder": "缴费提醒",
    "notification.event_invitation": "活动邀请",
  },
  "en-US": {
    "common.loading": "Loading...",
    "common.error": "Failed to load",
    "common.retry": "Retry",
    "common.save": "Save",
    "common.cancel": "Cancel",
    "common.confirm": "Confirm",

    "nav.dashboard": "Dashboard",
    "nav.grades": "Grades",
    "nav.attendance": "Attendance",
    "nav.homework": "Homework",
    "nav.weakness": "Analysis",
    "nav.notifications": "Notifications",
    "nav.preferences": "Preferences",

    "child.switcher.empty": "No child bound",
    "child.switcher.select": "Select child",

    "error.iam.invalid_credentials": "Invalid email or password",
    "error.iam.invalid_refresh": "Invalid refresh token",
    "error.iam.token_expired": "Token expired",
    "error.parent.child_not_found": "Child not found",
    "error.parent.permission_denied": "Permission denied",
    "error.parent.network_error": "Network error",
    "error.parent.graphql_error": "Failed to load data",

    "notification.grade_recorded": "Grade Published",
    "notification.homework_graded": "Homework Graded",
    "notification.homework_assigned": "Homework Assigned",
    "notification.exam_published": "Exam Published",
    "notification.attendance_alert": "Attendance Alert",
    "notification.school_announcement": "School Announcement",
    "notification.teacher_message": "Teacher Message",
    "notification.fee_reminder": "Fee Reminder",
    "notification.event_invitation": "Event Invitation",
  },
};

export function getLocale(): Locale {
  if (typeof navigator !== "undefined") {
    const lang = navigator.language;
    if (lang.startsWith("en")) return "en-US";
  }
  return DEFAULT_LOCALE;
}

export function t(key: string, locale: Locale = getLocale()): string {
  return (
    translations[locale]?.[key] ?? translations[DEFAULT_LOCALE]?.[key] ?? key
  );
}

export { translations };
