"use client";

import * as React from "react";
import { toast } from "sonner";
import { Ban, RotateCcw } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Spinner } from "@/components/ui/spinner";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import {
  cancelOrderAction,
  refillOrderAction,
  type OrderOpResult,
} from "@/lib/actions/order-ops";
import { ordersTexts } from "@/lib/i18n/orders";

function report(result: OrderOpResult) {
  if (result.ok) toast.success(result.data?.message ?? ordersTexts.refillSuccess);
  else toast.error(result.error);
}

export function OrderRowActions({
  orderId,
  canRefill,
  canCancel,
  refillPending,
  cancelPending,
}: {
  orderId: number;
  canRefill: boolean;
  canCancel: boolean;
  refillPending: boolean;
  cancelPending: boolean;
}) {
  const [busy, startTransition] = React.useTransition();
  const [open, setOpen] = React.useState(false);

  if (refillPending || cancelPending) {
    return (
      <span className="text-xs whitespace-nowrap text-muted-foreground">
        {refillPending ? ordersTexts.refillOpen : ordersTexts.cancelOpen}
      </span>
    );
  }

  if (!canRefill && !canCancel) {
    return <span className="text-xs text-muted-foreground">—</span>;
  }

  function runRefill() {
    startTransition(async () => {
      report(await refillOrderAction(orderId));
    });
  }

  function runCancel() {
    setOpen(false);
    startTransition(async () => {
      report(await cancelOrderAction(orderId));
    });
  }

  return (
    <div className="flex items-center justify-end gap-1.5">
      {canRefill ? (
        <Button
          type="button"
          size="sm"
          variant="outline"
          disabled={busy}
          onClick={runRefill}
          className="h-7 gap-1.5 rounded-lg px-2.5 text-xs"
        >
          {busy ? <Spinner className="size-3" /> : <RotateCcw className="size-3" />}
          {busy ? ordersTexts.refillPending : ordersTexts.refill}
        </Button>
      ) : null}

      {canCancel ? (
        <AlertDialog open={open} onOpenChange={setOpen}>
          <AlertDialogTrigger asChild>
            <Button
              type="button"
              size="sm"
              variant="ghost"
              disabled={busy}
              className="h-7 gap-1.5 rounded-lg px-2.5 text-xs text-destructive hover:bg-destructive/10 hover:text-destructive"
            >
              <Ban className="size-3" />
              {busy ? ordersTexts.cancelPending : ordersTexts.cancel}
            </Button>
          </AlertDialogTrigger>
          <AlertDialogContent>
            <AlertDialogHeader>
              <AlertDialogTitle className="font-display">
                {ordersTexts.cancelConfirmTitle}
              </AlertDialogTitle>
              <AlertDialogDescription>{ordersTexts.cancelConfirmBody}</AlertDialogDescription>
            </AlertDialogHeader>
            <AlertDialogFooter>
              <AlertDialogCancel>{ordersTexts.keepOrder}</AlertDialogCancel>
              <AlertDialogAction onClick={runCancel}>
                {ordersTexts.cancelConfirmAction}
              </AlertDialogAction>
            </AlertDialogFooter>
          </AlertDialogContent>
        </AlertDialog>
      ) : null}
    </div>
  );
}

export default OrderRowActions;
