"use client";

import React from "react";
import { X } from "lucide-react";
import * as Dialog from "@radix-ui/react-dialog";
import { cn } from "@/lib/utils";

interface HelpModalProps {
  /**
   * Contrôle l'ouverture du modal
   */
  open?: boolean;
  /**
   * Callback lors de la fermeture
   */
  onOpenChange?: (open: boolean) => void;
  /**
   * Titre du modal
   */
  title: string;
  /**
   * Contenu du modal
   */
  children: React.ReactNode;
  /**
   * Classe CSS personnalisée pour le contenu
   */
  contentClassName?: string;
  /**
   * Taille du modal
   * @default "md"
   */
  size?: "sm" | "md" | "lg" | "xl" | "full";
}

const sizeClasses = {
  sm: "max-w-md",
  md: "max-w-lg",
  lg: "max-w-2xl",
  xl: "max-w-4xl",
  full: "max-w-6xl",
};

export function HelpModal({
  open,
  onOpenChange,
  title,
  children,
  contentClassName,
  size = "md",
}: HelpModalProps) {
  return (
    <Dialog.Root open={open} onOpenChange={onOpenChange}>
      <Dialog.Portal>
        <Dialog.Overlay
          className={cn(
            "fixed inset-0 bg-black/50 backdrop-blur-sm z-50",
            "transition-opacity duration-200",
            "data-[state=open]:opacity-100",
            "data-[state=closed]:opacity-0"
          )}
        />
        <Dialog.Content
          className={cn(
            "fixed left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2",
            "w-full",
            sizeClasses[size],
            "bg-white rounded-2xl shadow-2xl z-50",
            "border border-slate-200",
            "transition-all duration-200",
            "data-[state=open]:animate-in",
            "data-[state=closed]:animate-out",
            "data-[state=closed]:fade-out-0",
            "data-[state=open]:fade-in-0",
            "data-[state=closed]:zoom-out-95",
            "data-[state=open]:zoom-in-95",
            "data-[state=closed]:slide-out-to-left-1/2",
            "data-[state=closed]:slide-out-to-top-[48%]",
            "data-[state=open]:slide-in-from-left-1/2",
            "data-[state=open]:slide-in-from-top-[48%]"
          )}
        >
          {/* Header */}
          <div className="flex items-center justify-between px-6 py-4 border-b border-slate-200">
            <Dialog.Title className="text-lg font-bold text-slate-900">
              {title}
            </Dialog.Title>
            <Dialog.Close
              className={cn(
                "w-8 h-8 rounded-lg flex items-center justify-center",
                "text-slate-400 hover:text-slate-700 hover:bg-slate-100",
                "transition-colors duration-150",
                "focus:outline-none focus:ring-2 focus:ring-indigo-500"
              )}
            >
              <X className="w-5 h-5" />
            </Dialog.Close>
          </div>

          {/* Content */}
          <div className={cn("px-6 py-4 max-h-[60vh] overflow-y-auto", contentClassName)}>
            {children}
          </div>
        </Dialog.Content>
      </Dialog.Portal>
    </Dialog.Root>
  );
}
