"use client";
import { useState } from "react";
import { useSession } from "next-auth/react";
import { Sparkles } from "lucide-react";
import WelcomeAnimation from "@/components/WelcomeAnimation";

export default function ReplayWelcomeButton() {
  const { data: session } = useSession();
  const [showAnim, setShowAnim] = useState(false);
  const [config, setConfig] = useState<any>(null);
  const [loading, setLoading] = useState(false);

  if (!session?.user) return null; // registered members only

  async function replay() {
    setLoading(true);
    const res = await fetch("/api/welcome-animation/config");
    const data = await res.json();
    setLoading(false);
    if (!data.imageUrl && !data.musicUrl) return; // nothing configured, nothing to show
    setConfig(data);
    setShowAnim(true);
  }

  return (
    <div className="flex justify-center mt-8 mb-4">
      <button
        onClick={replay}
        disabled={loading}
        className="flex items-center gap-2 bg-gradient-to-r from-amber-500 to-red-600 hover:from-amber-400 hover:to-red-500 text-white text-sm font-semibold px-6 py-3 rounded-full shadow-lg shadow-red-900/30 transition-all hover:scale-105 active:scale-95"
      >
        <Sparkles size={16} /> {loading ? "Loading..." : "Replay Welcome Animation"}
      </button>

      {showAnim && config && (
        <WelcomeAnimation previewConfig={config} onClosePreview={() => setShowAnim(false)} />
      )}
    </div>
  );
}
