"use client";
import { useState, useEffect } from "react";
import { useSession } from "next-auth/react";
import { Bell, BellRing, DollarSign, Crown } from "lucide-react";
import { toast } from "@/components/ui/Toaster";
import { useRouter } from "next/navigation";

export function ChannelActions({ channelId }: { channelId: string }) {
  const { data: session } = useSession();
  const router = useRouter();
  const [following, setFollowing] = useState(false);
  const [loading, setLoading] = useState(true);
  const [showTipModal, setShowTipModal] = useState(false);

  useEffect(() => {
    if (!session) { setLoading(false); return; }
    fetch(`/api/channel/${channelId}/follow`)
      .then(r => r.json())
      .then(d => { setFollowing(d.following); setLoading(false); })
      .catch(() => setLoading(false));
  }, [channelId, session]);

  const handleFollow = async () => {
    if (!session) return router.push("/login");
    setLoading(true);
    const res = await fetch(`/api/channel/${channelId}/follow`, { method: "POST" });
    if (res.ok) {
      const d = await res.json();
      setFollowing(d.following);
      toast(d.following ? "Vous êtes abonné à cette chaîne !" : "Abonnement annulé.");
    }
    setLoading(false);
  };

  return (
    <>
      <div className="flex flex-wrap gap-3">
        <button
          disabled={loading}
          onClick={handleFollow}
          className={`flex items-center gap-2 px-5 py-2.5 rounded-full font-bold text-sm transition-all duration-300 shadow-sm ${
            following 
              ? "bg-[#12080b] text-[#aaaaaa] border border-[#2e1217] hover:text-white hover:bg-[#1a0a0f]" 
              : "bg-[#e05a68]/10 text-[#e05a68] border border-[#e05a68]/30 hover:bg-[#e05a68]/20 hover:border-[#e05a68] shadow-[0_0_15px_rgba(224,90,104,0.2)] hover:shadow-[0_0_20px_rgba(224,90,104,0.4)] scale-105"
          }`}
        >
          {following ? <><BellRing size={18} /> Abonné</> : <><Bell size={18} /> S'abonner</>}
        </button>

        <button 
          onClick={() => setShowTipModal(true)}
          className="flex items-center gap-2 px-5 py-2.5 rounded-full font-bold text-sm bg-gradient-to-r from-amber-500/10 to-yellow-600/10 text-amber-500 border border-amber-500/30 hover:bg-amber-500/20 hover:border-amber-500 hover:shadow-[0_0_20px_rgba(245,158,11,0.4)] transition-all duration-300 scale-105"
        >
          <DollarSign size={18} /> Soutenir
        </button>
      </div>

      {showTipModal && (
        <div className="fixed inset-0 z-[100] flex items-center justify-center bg-black/80 p-4">
          <div className="bg-[#12080b] border border-[#2e1217] rounded-xl p-8 max-w-sm w-full text-center">
            <div className="w-16 h-16 rounded-full bg-gradient-to-r from-yellow-500 to-amber-600 flex items-center justify-center mx-auto mb-4 text-black">
              <DollarSign size={32} />
            </div>
            <h3 className="text-xl font-bold mb-2">Envoyer un pourboire</h3>
            <p className="text-sm text-[#aaaaaa] mb-6">Récompensez le créateur pour son travail.</p>
            
            <div className="grid grid-cols-3 gap-2 mb-6">
              {[5, 10, 20].map(amount => (
                <button key={amount} className="py-2 rounded bg-[#070406] border border-[#2e1217] hover:border-[#e05a68] text-lg font-bold transition-colors">
                  {amount}€
                </button>
              ))}
            </div>
            
            <p className="text-xs text-amber-500 flex items-center justify-center gap-1 mb-6">
              <Crown size={12} /> Exclusif VIP Premium
            </p>
            
            <div className="flex gap-3">
              <button onClick={() => setShowTipModal(false)} className="flex-1 py-2 rounded border border-[#2e1217] text-[#aaaaaa] hover:text-white transition-colors">Annuler</button>
              <button onClick={() => { setShowTipModal(false); toast("Fonctionnalité de paiement à venir !", "success"); }} className="flex-1 py-2 rounded bg-white text-black font-bold">Confirmer</button>
            </div>
          </div>
        </div>
      )}
    </>
  );
}
