import Link from "next/link"
import { CalendarClock, ClipboardList, Settings2 } 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 { 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 async function generateMetadata(): Promise<Metadata> {
  const t = await getTranslations("scheduling")
  return {
    title: `${t("auto.title")} - Next_Edu`,
    description: t("auto.description"),
  }
}

export const dynamic = "force-dynamic"

export default async function AdminSchedulingAutoPage(): Promise<JSX.Element> {
  const t = await getTranslations("scheduling")
  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">{t("auto.title")}</h2>
          <p className="text-muted-foreground">{t("auto.description")}</p>
        </div>
        <Button asChild variant="outline">
          <Link href="/admin/scheduling/rules">
            <Settings2 className="mr-2 h-4 w-4" />
            {t("auto.configureRules")}
          </Link>
        </Button>
      </div>

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

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