"use client";

import { useEffect, useState, type ReactNode } from "react";
import { createPortal } from "react-dom";
import { BoltIcon } from "./icons";

const isBrowser = typeof document !== "undefined";

type ComingSoonButtonProps = {
  children: ReactNode;
  /** Button classes — pass the same styling the original link used. */
  className?: string;
  /** Toast copy shown on click. */
  message?: string;
};

/**
 * A button that, instead of navigating, surfaces an on-brand "coming soon"
 * toast. The toast is portaled to <body> so it escapes any ancestor that
 * establishes a containing block (e.g. the navbar's backdrop-blur) and stays
 * pinned to the viewport. Auto-dismisses after a few seconds.
 */
export default function ComingSoonButton({
  children,
  className,
  message = "This feature is coming soon. Stay tuned!",
}: ComingSoonButtonProps) {
  const [show, setShow] = useState(false);

  useEffect(() => {
    if (!show) return;
    const timer = setTimeout(() => setShow(false), 3200);
    return () => clearTimeout(timer);
  }, [show]);

  return (
    <>
      <button
        type="button"
        onClick={() => setShow(true)}
        className={`cursor-pointer ${className ?? ""}`}
      >
        {children}
      </button>

      {isBrowser &&
        show &&
        createPortal(
          <div
            role="status"
            aria-live="polite"
            className="pointer-events-none fixed inset-x-0 bottom-24 z-[100] flex justify-center px-4 md:bottom-6"
          >
            <div className="animate-toast-in flex max-w-sm items-center gap-3 rounded-full border border-white/10 bg-panel/95 px-5 py-3 text-sm font-medium text-white shadow-[0_24px_60px_-24px_rgba(123,108,253,0.7)] backdrop-blur-xl">
              <span className="flex h-6 w-6 shrink-0 items-center justify-center rounded-full bg-gradient-to-br from-brand to-electric text-white">
                <BoltIcon width={14} height={14} />
              </span>
              {message}
            </div>
          </div>,
          document.body
        )}
    </>
  );
}
