"use client";

import { ChevronLeft, ChevronRight, Crown } from "lucide-react";
import { useRef, useState } from "react";
import Link from "next/link";

interface Creator {
  id: string;
  name: string;
  image: string;
  isOnline: boolean;
  rank: number;
}

interface CreatorCarouselProps {
  title: string;
  creators: Creator[];
}

export function CreatorCarousel({ title, creators }: CreatorCarouselProps) {
  const rowRef = useRef<HTMLDivElement>(null);
  const [isMoved, setIsMoved] = useState(false);

  const handleClick = (direction: "left" | "right") => {
    if (rowRef.current) {
      const { scrollLeft, clientWidth } = rowRef.current;
      const scrollTo = direction === "left" ? scrollLeft - clientWidth : scrollLeft + clientWidth;
      rowRef.current.scrollTo({ left: scrollTo, behavior: "smooth" });
      setIsMoved(true);
    }
  };

  if (!creators || creators.length === 0) return null;

  return (
    <div 
      style={{ marginBottom: "var(--custom-section-spacing, 32px)" }}
      className="relative group transition-all duration-150"
    >
      <div className="flex items-center justify-between mb-5 px-4 lg:px-8">
        <div className="flex items-center gap-3">
          <div className="flex items-center gap-2">
            <Crown size={22} className="text-[#e05a68] fill-[#e05a68]/20 animate-pulse" />
            <h2 
              style={{ fontSize: "var(--custom-section-title-size, 22px)" }}
              className="font-black text-white tracking-tight"
            >
              {title}
            </h2>
          </div>
          <span className="bg-[#12080b] border border-[#2e1217] text-[#aaaaaa] text-xs font-bold px-2.5 py-1 rounded-full flex items-center gap-1 shadow-sm">
            ⏱️ Reset 3 jours
          </span>
        </div>
      </div>

      <div className="relative">
        <button
          className={`absolute left-4 top-1/2 -translate-y-1/2 z-20 w-12 h-12 rounded-full bg-black/70 border border-white/10 hover:bg-[#e05a68] hover:border-[#e05a68] hover:text-black hover:scale-110 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-all duration-300 backdrop-blur-md shadow-[0_0_20px_rgba(0,0,0,0.5)] ${
            !isMoved ? "hidden" : ""
          }`}
          onClick={() => handleClick("left")}
        >
          <ChevronLeft size={24} />
        </button>

        <div
          ref={rowRef}
          className="flex overflow-x-scroll scrollbar-hide px-4 lg:px-8 pb-6 pt-2 snap-x snap-mandatory"
          style={{ gap: "var(--custom-video-gap, 18px)", scrollbarWidth: "none", msOverflowStyle: "none" }}
        >
          {creators.map((creator) => (
            <Link 
              key={creator.id} 
              href={`/channel/${creator.id}`} 
              className="min-w-[190px] md:min-w-[230px] snap-start group/card relative overflow-hidden rounded-2xl border border-[#2e1217] hover:border-[#e05a68]/70 transition-all duration-500 hover:shadow-[0_10px_30px_rgba(224,90,104,0.3)] aspect-[3/4] bg-[#12080b]"
            >
              <img 
                src={creator.image} 
                alt={creator.name} 
                className="w-full h-full object-cover transition-transform duration-700 group-hover/card:scale-110" 
              />
              
              <div className="absolute top-3 left-3 right-3 flex justify-between items-center z-10">
                <span className="bg-gradient-to-r from-[#e05a68] to-[#c93f52] text-white text-[10px] font-black px-2.5 py-0.5 rounded-full shadow-[0_0_15px_rgba(224,90,104,0.5)] uppercase tracking-wider">
                  #{creator.rank} MODEL
                </span>
                <div className="flex items-center justify-center w-6 h-6 bg-black/60 backdrop-blur-md rounded-full border border-white/10 shadow-lg text-[11px]">
                  🇫🇷
                </div>
              </div>

              <div className="absolute bottom-0 inset-x-0 h-3/5 bg-gradient-to-t from-[#050508] via-[#050508]/70 to-transparent flex flex-col justify-end p-4">
                <div className="flex items-center gap-1.5 mb-1">
                  <span className={`w-2 h-2 rounded-full ${creator.isOnline ? "bg-emerald-500 shadow-[0_0_8px_#10b981] animate-pulse" : "bg-gray-500"}`} />
                  <span className={`text-[10px] font-extrabold uppercase tracking-wider ${creator.isOnline ? "text-emerald-400" : "text-gray-400"}`}>
                    {creator.isOnline ? "En Ligne" : "Hors Ligne"}
                  </span>
                </div>
                <h3 className="text-white font-black text-lg truncate group-hover/card:text-[#e05a68] transition-colors">{creator.name}</h3>
              </div>
            </Link>
          ))}
        </div>

        <div className="absolute top-0 right-0 bottom-0 w-24 bg-gradient-to-l from-[#050508] to-transparent pointer-events-none z-10" />
        <div className={`absolute top-0 left-0 bottom-0 w-24 bg-gradient-to-r from-[#050508] to-transparent pointer-events-none z-10 transition-opacity duration-300 ${isMoved ? "opacity-100" : "opacity-0"}`} />

        <button
          className="absolute right-4 top-1/2 -translate-y-1/2 z-20 w-12 h-12 rounded-full bg-black/70 border border-white/10 hover:bg-[#e05a68] hover:border-[#e05a68] hover:text-black hover:scale-110 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-all duration-300 backdrop-blur-md shadow-[0_0_20px_rgba(0,0,0,0.5)]"
          onClick={() => handleClick("right")}
        >
          <ChevronRight size={24} />
        </button>
      </div>
    </div>
  );
}

