import { redirect } from "next/navigation"

import { auth } from "@/auth"
import { Permissions } from "@/shared/types/permissions"
import { resolvePermissions } from "@/shared/lib/permissions"
import { isRole } from "@/shared/types/permissions"

export const dynamic = "force-dynamic"

export default async function DashboardPage(): Promise<void> {
  const session = await auth()
  if (!session?.user) redirect("/login")

  const roles = (session.user.roles ?? []).filter(isRole)
  const permissions = resolvePermissions(roles)

  // 按优先级匹配仪表盘权限（admin > student > parent > teacher）
  if (permissions.includes(Permissions.DASHBOARD_ADMIN_READ)) {
    redirect("/admin/dashboard")
  }
  if (permissions.includes(Permissions.DASHBOARD_STUDENT_READ)) {
    redirect("/student/dashboard")
  }
  if (permissions.includes(Permissions.DASHBOARD_PARENT_READ)) {
    redirect("/parent/dashboard")
  }
  redirect("/teacher/dashboard")
}
