import type { ReactNode } from "react"
import {
  LayoutDashboard,
  GraduationCap,
  CalendarDays,
  ClipboardList,
  PenTool,
  Library,
  FileQuestion,
} from "lucide-react"

import { QuickLinksCard, type QuickLinkItem } from "@/modules/settings/components/quick-links-card"
import type { Role } from "@/shared/types/permissions"

/**
 * 角色设置页配置
 *
 * 通过配置驱动角色 → 设置视图的映射，新增角色只需在此添加条目。
 * description/backHref 使用 i18n 键，由消费方翻译。
 * 快捷链接的 label 通过 settings.quickLinks 命名空间国际化。
 */

export interface RoleSettingsConfig {
  /** i18n 键：settings.<role>.description */
  descriptionKey: string
  backHref: string
  /** 角色专属快捷链接区块（General 标签页底部） */
  generalExtra?: ReactNode
}

const STUDENT_LINKS: QuickLinkItem[] = [
  { href: "/student/dashboard", labelKey: "dashboard", icon: <LayoutDashboard className="h-4 w-4" /> },
  { href: "/student/learning/assignments", labelKey: "assignments", icon: <PenTool className="h-4 w-4" /> },
  { href: "/student/schedule", labelKey: "schedule", icon: <CalendarDays className="h-4 w-4" /> },
]

const PARENT_LINKS: QuickLinkItem[] = [
  { href: "/parent/dashboard", labelKey: "dashboard", icon: <LayoutDashboard className="h-4 w-4" /> },
  { href: "/parent/children", labelKey: "children", icon: <GraduationCap className="h-4 w-4" /> },
  { href: "/parent/grades", labelKey: "grades", icon: <ClipboardList className="h-4 w-4" /> },
  { href: "/parent/attendance", labelKey: "attendance", icon: <CalendarDays className="h-4 w-4" /> },
]

const TEACHER_LINKS: QuickLinkItem[] = [
  { href: "/teacher/dashboard", labelKey: "dashboard", icon: <LayoutDashboard className="h-4 w-4" /> },
  { href: "/teacher/textbooks", labelKey: "textbooks", icon: <Library className="h-4 w-4" /> },
  { href: "/teacher/exams/all", labelKey: "exams", icon: <FileQuestion className="h-4 w-4" /> },
  { href: "/teacher/homework/assignments", labelKey: "homework", icon: <PenTool className="h-4 w-4" /> },
  { href: "/teacher/classes/schedule", labelKey: "schedule", icon: <CalendarDays className="h-4 w-4" /> },
]

export const ROLE_SETTINGS_CONFIG: Partial<Record<Role, RoleSettingsConfig>> = {
  admin: {
    descriptionKey: "roleDescriptions.admin",
    backHref: "/admin/dashboard",
  },
  teacher: {
    descriptionKey: "roleDescriptions.teacher",
    backHref: "/teacher/dashboard",
    generalExtra: <QuickLinksCard links={TEACHER_LINKS} />,
  },
  student: {
    descriptionKey: "roleDescriptions.student",
    backHref: "/student/dashboard",
    generalExtra: <QuickLinksCard links={STUDENT_LINKS} />,
  },
  parent: {
    descriptionKey: "roleDescriptions.parent",
    backHref: "/parent/dashboard",
    generalExtra: <QuickLinksCard links={PARENT_LINKS} />,
  },
}

/**
 * 根据角色列表解析首选设置配置。
 * 优先级：admin > teacher > student > parent
 */
export function resolveRoleSettingsConfig(roles: Role[]): RoleSettingsConfig | null {
  for (const role of roles) {
    const config = ROLE_SETTINGS_CONFIG[role]
    if (config) return config
  }
  return null
}
