import { CalendarCog, ClipboardList } from "lucide-react"
import type { Metadata } from "next"
import type { JSX } from "react"

import { requirePermission } from "@/shared/lib/auth-guard"
import { Permissions } from "@/shared/types/permissions"
import { EmptyState } from "@/shared/components/ui/empty-state"
import {
  getAdminClassesForScheduling,
  getSchedulingRules,
} from "@/modules/scheduling/data-access"
import { SchedulingRulesForm } from "@/modules/scheduling/components/scheduling-rules-form"

export const metadata: Metadata = {
  title: "排课规则 - Next_Edu",
  description: "配置每日课时上限、课间窗口与均衡偏好",
}

export const dynamic = "force-dynamic"

export default async function AdminSchedulingRulesPage(): Promise<JSX.Element> {
  await requirePermission(Permissions.SCHEDULE_ADJUST)
  const [classes, existingRules] = await Promise.all([
    getAdminClassesForScheduling(),
    getSchedulingRules(),
  ])

  const classOptions = classes.map((c) => ({ id: c.id, name: c.name, grade: c.grade }))

  return (
    <div className="flex h-full flex-col space-y-8 p-8">
      <div className="space-y-1">
        <h2 className="text-2xl font-bold tracking-tight">排课规则</h2>
        <p className="text-muted-foreground">
          配置每日课时上限、课间窗口与均衡偏好。
        </p>
      </div>

      {classOptions.length === 0 ? (
        <EmptyState
          icon={ClipboardList}
          title="暂无可用班级"
          description="请先创建班级，再配置排课规则。"
        />
      ) : (
        <SchedulingRulesForm classes={classOptions} existingRules={existingRules} />
      )}

      <div className="flex items-center gap-2 text-xs text-muted-foreground">
        <CalendarCog className="h-4 w-4" />
        <span>提示：未选择具体班级时保存的规则将作为全局默认。</span>
      </div>
    </div>
  )
}
