"use client";

import { useTransition } from "react";
import { useRouter } from "next/navigation";
import { handleCancelPurchaseReturn } from "@/lib/actions";
import { Button } from "@/components/ui/button";
import { useToast } from "@/hooks/use-toast";

export default function PurchaseReturnDetailActions({
  returnId,
  isCancelled,
  t = {},
}: {
  returnId: string;
  isCancelled: boolean;
  t?: Record<string, string>;
}) {
  const { toast } = useToast();
  const [isPending, startTransition] = useTransition();
  const router = useRouter();

  const cancel = () => {
    startTransition(async () => {
      const result = await handleCancelPurchaseReturn(returnId);
      if (result?.success) {
        toast({ title: t.cancelSuccess || "تم إلغاء المرتجع" });
        router.refresh();
      } else {
        toast({ title: t.error || "خطأ", description: result?.error || t.cancelFailed || "فشل الإلغاء", variant: "destructive" });
      }
    });
  };

  return (
    <div className="flex justify-center gap-2">
      <Button variant="destructive" size="sm" onClick={cancel} disabled={isPending || isCancelled}>
        {isPending ? (t.processing || "جارٍ التنفيذ") : (t.cancel || "إلغاء")}
      </Button>
    </div>
  );
}
