import { CalendarCog, ClipboardList } from "lucide-react"
import type { Metadata } from "next"
import type { JSX } from "react"
import { getTranslations } from "next-intl/server"

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 async function generateMetadata(): Promise<Metadata> {
  const t = await getTranslations("scheduling")
  return {
    title: `${t("rules.title")} - Next_Edu`,
    description: t("rules.description"),
  }
}

export const dynamic = "force-dynamic"

export default async function AdminSchedulingRulesPage(): Promise<JSX.Element> {
  const t = await getTranslations("scheduling")
  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">{t("rules.title")}</h2>
        <p className="text-muted-foreground">{t("rules.description")}</p>
      </div>

      {classOptions.length === 0 ? (
        <EmptyState
          icon={ClipboardList}
          title={t("rules.noClassesTitle")}
          description={t("rules.noClassesDescription")}
        />
      ) : (
        <SchedulingRulesForm classes={classOptions} existingRules={existingRules} />
      )}

      <div className="flex items-center gap-2 text-xs text-muted-foreground">
        <CalendarCog className="h-4 w-4" />
        <span>{t("rules.hint")}</span>
      </div>
    </div>
  )
}
