"use client";

import { useRouter } from "next/navigation";

export function ProjectActions({ id, isDefault }: { id: string; isDefault: boolean }) {
  const router = useRouter();

  async function rename() {
    const name = prompt("Project name");
    if (!name) return;
    await fetch(`/api/projects/${id}`, {
      method: "PATCH",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ name })
    });
    router.refresh();
  }

  async function remove() {
    if (isDefault || !confirm("Delete this project? Used projects will be archived.")) return;
    await fetch(`/api/projects/${id}`, { method: "DELETE" });
    router.refresh();
  }

  return (
    <div className="flex gap-2">
      <button className="rounded-lg border border-slate-300 px-3 py-1 text-xs font-semibold" onClick={() => void rename()} type="button">Edit</button>
      <button className="rounded-lg bg-brand-primary px-3 py-1 text-xs font-semibold text-white disabled:opacity-40" disabled={isDefault} onClick={() => void remove()} type="button">Delete</button>
    </div>
  );
}
