"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import { toast } from "sonner";
import { Ban } from "lucide-react";
import { Button } from "@/components/ui/button";

export function BlockButton({
  targetUsername,
  initialBlocked = false,
}: {
  targetUsername: string;
  initialBlocked?: boolean;
}) {
  const router = useRouter();
  const [blocked, setBlocked] = useState(initialBlocked);
  const [loading, setLoading] = useState(false);

  async function toggle() {
    if (!blocked && !confirm(`Block @${targetUsername}? They won't be able to follow you or comment on your recipes.`)) {
      return;
    }
    setLoading(true);
    try {
      const res = await fetch(`/api/v1/users/${targetUsername}/block`, {
        method: blocked ? "DELETE" : "POST",
      });
      if (!res.ok) {
        const err = await res.json() as { error?: string };
        toast.error(err.error ?? "Failed");
        return;
      }
      setBlocked(!blocked);
      toast.success(blocked ? "Unblocked" : "Blocked");
      router.refresh();
    } finally {
      setLoading(false);
    }
  }

  return (
    <Button
      variant="outline"
      size="sm"
      onClick={() => { void toggle(); }}
      disabled={loading}
      className={blocked ? "" : "text-destructive hover:text-destructive"}
    >
      <Ban className="h-3.5 w-3.5" />
      {loading ? "…" : blocked ? "Unblock" : "Block"}
    </Button>
  );
}
