import React, { useState, useRef, useEffect } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { ChevronLeft, ChevronRight } from 'lucide-react';

const SidebarTabs = ({ tabs, onTabChange }) => {
  const [activeTab, setActiveTab] = useState(tabs[0].id);
  const [showLeftArrow, setShowLeftArrow] = useState(false);
  const [showRightArrow, setShowRightArrow] = useState(false);
  const scrollRef = useRef(null);

  const handleTabChange = (tabId) => {
    setActiveTab(tabId);
    if (onTabChange) {
      onTabChange(tabId);
    }
  };

  const updateArrows = () => {
    const el = scrollRef.current;
    if (!el) return;
    setShowLeftArrow(el.scrollLeft > 0);
    setShowRightArrow(el.scrollLeft + el.clientWidth < el.scrollWidth - 1);
  };

  useEffect(() => {
    updateArrows();
    const el = scrollRef.current;
    if (!el) return;
    el.addEventListener('scroll', updateArrows);
    window.addEventListener('resize', updateArrows);
    return () => {
      el.removeEventListener('scroll', updateArrows);
      window.removeEventListener('resize', updateArrows);
    };
  }, [tabs]);

  const scroll = (direction) => {
    const el = scrollRef.current;
    if (!el) return;
    el.scrollBy({ left: direction === 'left' ? -150 : 150, behavior: 'smooth' });
  };

  return (
    <div className="flex flex-col h-full w-full">
      <div className="relative flex items-center bg-neutral border-b border-gray-200 shadow-sm">
        {showLeftArrow && (
          <button
            onClick={() => scroll('left')}
            className="absolute left-0 z-10 h-full w-10 flex items-center justify-center bg-gradient-to-r from-neutral via-neutral to-transparent text-gray-500 hover:text-primary active:text-secondary"
            aria-label="Tabs pr\u00e9c\u00e9dents"
          >
            <ChevronLeft size={22} strokeWidth={2.5} />
          </button>
        )}

        <div
          ref={scrollRef}
          className="flex overflow-x-auto scrollbar-none scroll-smooth"
          style={{ scrollbarWidth: 'none', msOverflowStyle: 'none' }}
        >
          {tabs.map((tab) => (
            <button
              key={tab.id}
              onClick={() => handleTabChange(tab.id)}
              className={`flex-shrink-0 whitespace-nowrap h-14 px-5 font-label font-medium transition-colors duration-200 min-h-[44px] ${
                activeTab === tab.id
                  ? 'border-b-4 border-primary text-primary bg-primary/5 font-semibold'
                  : 'text-gray-500 hover:text-primary'
              }`}
            >
              {tab.label}
            </button>
          ))}
        </div>

        {showRightArrow && (
          <button
            onClick={() => scroll('right')}
            className="absolute right-0 z-10 h-full w-10 flex items-center justify-center bg-gradient-to-l from-neutral via-neutral to-transparent text-gray-500 hover:text-primary active:text-secondary"
            aria-label="Tabs suivants"
          >
            <ChevronRight size={22} strokeWidth={2.5} />
          </button>
        )}
      </div>

      <div className="flex-1 flex flex-col overflow-hidden rounded-b-md shadow-inner">
        <AnimatePresence mode="wait">
          {tabs.map(
            (tab) =>
              activeTab === tab.id && (
                <motion.div
                  key={tab.id}
                  initial={{ opacity: 0, x: 50 }}
                  animate={{ opacity: 1, x: 0 }}
                  exit={{ opacity: 0, x: -50 }}
                  transition={{ duration: 0.3 }}
                  className="flex-1 flex flex-col h-full min-h-0"
                >
                  {tab.content}
                </motion.div>
              )
          )}
        </AnimatePresence>
      </div>
    </div>
  );
};

export default SidebarTabs;
