"use client";

import React, { useEffect } from "react";
import { cn } from "@/lib/utils";
import { glassBackdropStyle } from "@/lib/glassBackdrop";

type ModalProps = {
  isOpen: boolean;
  onClose: () => void;
  children: React.ReactNode;
  /** ارتفاع ثابت پنل — برای auto/max از panelClassName استفاده کنید */
  height?: string;
  /** بالاتر از منوبار (z-50) */
  elevated?: boolean;
  panelClassName?: string;
};

const Modal: React.FC<ModalProps> = ({
  isOpen,
  onClose,
  children,
  height = "300px",
  elevated = false,
  panelClassName,
}) => {
  useEffect(() => {
    if (isOpen) {
      document.body.style.overflow = "hidden";
    } else {
      document.body.style.overflow = "auto";
    }
    return () => {
      document.body.style.overflow = "auto";
    };
  }, [isOpen]);

  if (!isOpen) return null;

  return (
    <div
      onClick={onClose}
      className={cn(
        "glass-modal-overlay fixed inset-0 flex items-end",
        elevated ? "z-[220]" : "z-40"
      )}
      style={glassBackdropStyle("glass-modal-overlay")}
    >
      <div
        className={cn(
          "glass-modal-panel glass-modal-panel--sheet relative container mx-auto w-full p-5",
          panelClassName
        )}
        style={{
          ...glassBackdropStyle("glass-modal-panel"),
          ...(height === "auto" || height === "fit" ? undefined : { height }),
        }}
        onClick={(e) => e.stopPropagation()}
      >
        {children}
      </div>
    </div>
  );
};

export default Modal;
