"use client";

import { useState } from "react";
import { createClient } from "@/lib/supabase/client";

export function ResetRoundButton() {
  const [state, setState] = useState<"idle" | "loading" | "done">("idle");

  async function handleClick() {
    setState("loading");
    const supabase = createClient();
    const { error } = await supabase.rpc("reset_rank_reference");
    setState(error ? "idle" : "done");
    if (!error) {
      setTimeout(() => setState("idle"), 2000);
    }
  }

  return (
    <button
      type="button"
      onClick={handleClick}
      disabled={state === "loading"}
      className="marble-surface rounded-md border border-gold/40 px-3 py-1.5 text-sm font-medium text-text-marble shadow-sm transition hover:bg-gold/10 disabled:opacity-50"
    >
      {state === "loading" ? "…" : state === "done" ? "Repère mis à jour ✓" : "Nouveau round (réinitialiser le repère)"}
    </button>
  );
}
