import Link from "next/link"
import { CalendarClock, ClipboardList, Settings2 } 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 { Button } from "@/shared/components/ui/button"
import { EmptyState } from "@/shared/components/ui/empty-state"
import { getAdminClassesForScheduling } from "@/modules/scheduling/data-access"
import { AutoSchedulePanel } from "@/modules/scheduling/components/auto-schedule-panel"

export const metadata: Metadata = {
  title: "自动排课 - Next_Edu",
  description: "基于规则与学科分配自动生成周课表",
}

export const dynamic = "force-dynamic"

export default async function AdminSchedulingAutoPage(): Promise<JSX.Element> {
  await requirePermission(Permissions.SCHEDULE_AUTO)
  const classes = await getAdminClassesForScheduling()
  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="flex items-center justify-between space-y-2">
        <div className="space-y-1">
          <h2 className="text-2xl font-bold tracking-tight">自动排课</h2>
          <p className="text-muted-foreground">
            基于规则与学科分配自动生成周课表。
          </p>
        </div>
        <Button asChild variant="outline">
          <Link href="/admin/scheduling/rules">
            <Settings2 className="mr-2 h-4 w-4" />
            配置规则
          </Link>
        </Button>
      </div>

      {classOptions.length === 0 ? (
        <EmptyState
          icon={ClipboardList}
          title="暂无可用班级"
          description="请先创建班级，再进行自动排课。"
        />
      ) : (
        <AutoSchedulePanel classes={classOptions} />
      )}

      <div className="flex items-center gap-2 text-xs text-muted-foreground">
        <CalendarClock className="h-4 w-4" />
        <span>应用新课表将替换所选班级的现有课表。</span>
      </div>
    </div>
  )
}
