"use client"

import type { JSX } from "react"
import { useTranslations } from "next-intl"

import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@/shared/components/ui/alert-dialog"

export type PendingSwitch =
  | { kind: "exam"; examId: string; message: string }
  | { kind: "class"; classId: string; message: string }
  | null

interface Props {
  pendingSwitch: PendingSwitch
  onConfirm: () => void
  onCancel: () => void
}

/**
 * 切换试卷/班级确认对话框。
 *
 * P1-6 重构：从 batch-grade-entry.tsx 拆分而来。
 * 替代原来的 window.confirm()，支持无障碍访问。
 */
export function BatchGradeEntryDialog({
  pendingSwitch,
  onConfirm,
  onCancel,
}: Props): JSX.Element {
  const t = useTranslations("grades")

  return (
    <AlertDialog
      open={pendingSwitch !== null}
      onOpenChange={(open) => {
        if (!open) onCancel()
      }}
    >
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>{t("batchByExam.confirmTitle")}</AlertDialogTitle>
          <AlertDialogDescription>
            {pendingSwitch?.message ?? ""}
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>{t("batchByExam.cancelAction")}</AlertDialogCancel>
          <AlertDialogAction onClick={onConfirm}>
            {t("batchByExam.confirmAction")}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}
