"use client";

import { useTranslation } from "react-i18next";

import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog";
import { DrawPublishPanel } from "@/modules/draws/draw-publish-panel";
import type { AdminDrawBatchRow } from "@/types/api/admin-draws";

type DrawPublishDialogProps = {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  drawId: number;
  batch: AdminDrawBatchRow | null;
  onPublished: () => void;
  onDiscarded: () => void;
};

export function DrawPublishDialog({
  open,
  onOpenChange,
  drawId,
  batch,
  onPublished,
  onDiscarded,
}: DrawPublishDialogProps): React.ReactElement {
  const { t } = useTranslation("draws");

  return (
    <Dialog open={open && batch != null} onOpenChange={onOpenChange}>
      <DialogContent className="flex max-h-[min(90vh,40rem)] w-full max-w-lg flex-col gap-0 overflow-hidden p-0 sm:max-w-lg">
        <DialogHeader className="border-b border-border/60 px-5 py-4">
          <DialogTitle className="text-base">
            {t("publishTitle")}
            {batch ? ` · v${batch.result_version}` : ""}
          </DialogTitle>
        </DialogHeader>
        <div className="min-h-0 flex-1 overflow-y-auto px-5 py-4">
          {batch ? (
            <DrawPublishPanel
              drawId={drawId}
              batch={batch}
              onPublished={() => {
                onPublished();
                onOpenChange(false);
              }}
              onDiscarded={() => {
                onDiscarded();
                onOpenChange(false);
              }}
            />
          ) : null}
        </div>
      </DialogContent>
    </Dialog>
  );
}