"use client";

import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { deleteArticleCategory } from "@/lib/api/dashboard/article-categories-api";
import { useTranslations } from "next-intl";
import { useState } from "react";
import { toast } from "react-toastify";

export function CategoryDeleteDialog({
  open,
  onOpenChange,
  categoryId,
}: {
  open: boolean;
  onOpenChange: (v: boolean) => void;
  categoryId: number | null;
}) {
  const t = useTranslations("dashboard.blogs.categories");
  const tc = useTranslations("dashboard.blogs.common");
  const [pending, setPending] = useState(false);

  const confirm = async () => {
    if (!categoryId) return;
    setPending(true);
    try {
      await deleteArticleCategory(categoryId);
      toast.success("Deleted");
      onOpenChange(false);
    } catch (e) {
      toast.error(e instanceof Error ? e.message : "Error");
    } finally {
      setPending(false);
    }
  };

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>{t("deleteTitle")}</DialogTitle>
        </DialogHeader>
        <p className="text-sm text-muted-foreground">{t("deleteMessage")}</p>
        <DialogFooter>
          <Button variant="outline" onClick={() => onOpenChange(false)} disabled={pending}>
            {tc("cancel")}
          </Button>
          <Button variant="destructive" onClick={confirm} disabled={pending}>
            {tc("save")}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}
