"use client";

import React, { useState } from "react";
import { HelpCircle, X } from "lucide-react";
import { cn } from "@/lib/utils";

interface HelpButtonProps {
  /**
   * Contenu de l'aide à afficher
   */
  title: string;
  content: string | React.ReactNode;
  /**
   * Position du tooltip
   * @default "top"
   */
  position?: "top" | "bottom" | "left" | "right";
  /**
   * Variante du bouton
   * @default "icon"
   */
  variant?: "icon" | "text" | "inline";
  /**
   * Classe CSS personnalisée
   */
  className?: string;
  /**
   * Si vrai, le tooltip est toujours visible
   * @default false
   */
  alwaysOpen?: boolean;
}

export function HelpButton({
  title,
  content,
  position = "top",
  variant = "icon",
  className,
  alwaysOpen = false,
}: HelpButtonProps) {
  const [isOpen, setIsOpen] = useState(alwaysOpen);

  const positionClasses = {
    top: "bottom-full left-1/2 -translate-x-1/2 mb-3",
    bottom: "top-full left-1/2 -translate-x-1/2 mt-3",
    left: "right-full top-1/2 -translate-y-1/2 mr-3",
    right: "left-full top-1/2 -translate-y-1/2 ml-3",
  };

  const arrowClasses = {
    top: "top-full left-1/2 -translate-x-1/2 -mt-1 border-t-slate-900 border-r-transparent border-b-transparent border-l-transparent",
    bottom: "bottom-full left-1/2 -translate-x-1/2 -mb-1 border-b-slate-900 border-r-transparent border-t-transparent border-l-transparent",
    left: "left-full top-1/2 -translate-y-1/2 -ml-1 border-l-slate-900 border-t-transparent border-r-transparent border-b-transparent",
    right: "right-full top-1/2 -translate-y-1/2 -mr-1 border-r-slate-900 border-t-transparent border-l-transparent border-b-transparent",
  };

  const buttonVariants = {
    icon: (
      <button
        onClick={() => setIsOpen(!isOpen)}
        className={cn(
          "inline-flex items-center justify-center w-5 h-5 rounded-full transition-all duration-200",
          "text-slate-400 hover:text-indigo-600 hover:bg-indigo-50",
          "focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-1",
          isOpen && "bg-indigo-100 text-indigo-700",
          className
        )}
        aria-label={`Aide: ${title}`}
      >
        <HelpCircle className="w-4 h-4" />
      </button>
    ),
    text: (
      <button
        onClick={() => setIsOpen(!isOpen)}
        className={cn(
          "inline-flex items-center gap-1.5 px-2 py-1 rounded-md transition-all duration-200",
          "text-xs font-medium text-slate-500 hover:text-indigo-600 hover:bg-indigo-50",
          "focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-1",
          isOpen && "bg-indigo-100 text-indigo-700",
          className
        )}
        aria-label={`Aide: ${title}`}
      >
        <HelpCircle className="w-3.5 h-3.5" />
        <span>Aide</span>
      </button>
    ),
    inline: (
      <button
        onClick={() => setIsOpen(!isOpen)}
        className={cn(
          "inline-flex items-center gap-1 text-indigo-600 hover:text-indigo-700 underline underline-dotted underline-offset-2",
          "transition-all duration-200",
          "focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-1 rounded px-1",
          className
        )}
        aria-label={`Aide: ${title}`}
      >
        <span>{title}</span>
        <HelpCircle className="w-3 h-3" />
      </button>
    ),
  };

  return (
    <div className="relative inline-flex items-center">
      {buttonVariants[variant]}

      {isOpen && (
        <>
          {/* Backdrop */}
          <div
            className="fixed inset-0 z-40"
            onClick={() => setIsOpen(false)}
            aria-hidden="true"
          />

          {/* Tooltip */}
          <div
            className={cn(
              "absolute z-50 w-80 max-w-sm",
              positionClasses[position]
            )}
          >
            <div className="relative bg-slate-900 text-slate-100 rounded-lg shadow-xl p-4">
              {/* Arrow */}
              <div
                className={cn(
                  "absolute w-0 h-0 border-8",
                  arrowClasses[position]
                )}
                aria-hidden="true"
              />

              {/* Close button */}
              <button
                onClick={() => setIsOpen(false)}
                className="absolute top-2 right-2 text-slate-400 hover:text-white transition-colors"
                aria-label="Fermer"
              >
                <X className="w-4 h-4" />
              </button>

              {/* Content */}
              <div className="pr-6">
                <h4 className="text-sm font-bold text-white mb-2">{title}</h4>
                <div className="text-xs text-slate-300 leading-relaxed space-y-2">
                  {typeof content === "string" ? (
                    <p>{content}</p>
                  ) : (
                    content
                  )}
                </div>
              </div>
            </div>
          </div>
        </>
      )}
    </div>
  );
}
