﻿"use client";

import { VideoCard } from "@/components/VideoCard";
import { InFeedVideoAdCard } from "@/components/InFeedVideoAdCard";
import { ChevronLeft, ChevronRight, Flame } from "lucide-react";
import { useRef, useState } from "react";
import Link from "next/link";

interface HorizontalCarouselProps {
  title: string;
  videos: any[];
  viewAllLink?: string;
}

export function HorizontalCarousel({ title, videos, viewAllLink }: HorizontalCarouselProps) {
  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 (!videos || videos.length === 0) return null;

  const isTrending = title.toLowerCase().includes("tendance");

  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-2">
          {isTrending && <Flame size={24} className="text-[#f59e0b] fill-[#f59e0b]/20 animate-bounce" />}
          <h2 
            style={{ fontSize: "var(--custom-section-title-size, 22px)" }}
            className="font-black text-white tracking-tight"
          >
            {title}
          </h2>
        </div>
        {viewAllLink && (
          <Link 
            href={viewAllLink} 
            className="text-xs font-black uppercase tracking-wider text-[#e05a68] hover:text-white px-3 py-1.5 rounded-full bg-[#0e0800] border border-[#e05a68]/30 hover:border-[#e05a68] transition-all duration-300 shadow-sm"
          >
            Voir tout â†’
          </Link>
        )}
      </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}
          style={{ gap: "var(--custom-video-gap, 18px)", scrollbarWidth: "none", msOverflowStyle: "none" }}
          className="flex overflow-x-scroll scrollbar-hide px-4 lg:px-8 pb-8 pt-2 snap-x snap-mandatory"
        >
          {videos.filter((m) => m && m.id).map((media, idx) => (
            <div key={media.id} className="contents">
              {idx === 2 && (
                <div key="infeed-ad-card" className="min-w-[280px] md:min-w-[320px] lg:min-w-[350px] snap-start">
                  <InFeedVideoAdCard />
                </div>
              )}
              <div className="min-w-[280px] md:min-w-[320px] lg:min-w-[350px] snap-start">
                <VideoCard media={media} />
              </div>
            </div>
          ))}
        </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>
  );
}

