"use client";

import { useState } from "react";
import { ListPlus, Check } from "lucide-react";
import { motion, AnimatePresence } from "framer-motion";

export function SaveToPlaylist({ mediaId }: { mediaId: string }) {
  const [saved, setSaved] = useState(false);
  const [isAnimating, setIsAnimating] = useState(false);

  const handleSave = () => {
    // Dans une vraie app, on appellerait l'API ici
    setSaved(!saved);
    setIsAnimating(true);
    setTimeout(() => setIsAnimating(false), 1000);
  };

  return (
    <button 
      onClick={handleSave}
      className={`w-10 h-10 rounded-full flex items-center justify-center transition-all duration-300 relative ${
        saved 
          ? "bg-primary/20 text-primary border border-primary" 
          : "bg-surface border border-border text-textSecondary hover:text-primary hover:border-primary"
      }`}
      title={saved ? "Retirer" : "Regarder plus tard"}
    >
      <AnimatePresence mode="wait">
        {saved ? (
          <motion.div
            key="check"
            initial={{ scale: 0, rotate: -90 }}
            animate={{ scale: 1, rotate: 0 }}
            exit={{ scale: 0 }}
            transition={{ type: "spring", stiffness: 300, damping: 20 }}
          >
            <Check size={18} className="text-primary" />
          </motion.div>
        ) : (
          <motion.div
            key="plus"
            initial={{ scale: 0 }}
            animate={{ scale: 1 }}
            exit={{ scale: 0, rotate: 90 }}
          >
            <ListPlus size={18} />
          </motion.div>
        )}
      </AnimatePresence>
      
      {/* Animation particles */}
      {isAnimating && saved && (
        <motion.div 
          initial={{ scale: 1, opacity: 0.8 }}
          animate={{ scale: 1.5, opacity: 0 }}
          transition={{ duration: 0.5 }}
          className="absolute inset-0 rounded-full border-2 border-primary pointer-events-none"
        />
      )}
    </button>
  );
}
