"use client";

import {
  helpArticleUrl,
  openHelpLink,
  type HelpArticleId,
} from "@enode/core/help/articles";
import { useTranslation } from "@enode/core/translations/hooks";
import { Button } from "./button";
import { InfoIcon } from "./feedback/icons";
import { HeaderIconButton } from "./header-button";

/** Where the trigger sits — see {@link HowItWorksButton}. */
export type HowItWorksPlacement = "header" | "inline";

/**
 * The one entry point into a "How it works" walkthrough. Every surface that
 * opens a walkthrough uses this component, so the entry always reads the same:
 * an info icon and the words "How it works".
 *
 * @remarks
 * Two placements, chosen by where the trigger sits — not by taste:
 *
 * - `"header"` (default) — a page head or a drawer/dialog header. The head's
 *   secondary pill at ≥`sm` that collapses to its icon circle on phones (see
 *   `header-button.tsx`). Both forms are rendered and swapped by breakpoint
 *   class — no media-query JS, so SSR/hydration stay deterministic.
 * - `"inline"` — inside content: an empty state, a note, a builder step, a
 *   chart's empty message. A quiet muted text link with the same icon and
 *   label; it never competes with the surface's real actions.
 *
 * The label is fixed on purpose: one name for one kind of help.
 *
 * @param onClick - Opens the walkthrough.
 * @param placement - `"header"` or `"inline"`; defaults to `"header"`.
 * @param tourId - Spotlight-tour anchor (`data-tour`). Set on ONE wrapper
 *   around both breakpoint forms, so the tour never resolves the hidden one.
 * @param article - Optional help-centre article id. When set, a quiet "Read the
 *   guide" text link renders beside the button and opens that article (in-app
 *   browser on native, new tab on web).
 */
export function HowItWorksButton({
  onClick,
  placement = "header",
  tourId,
  article,
  className,
}: {
  onClick: () => void;
  placement?: HowItWorksPlacement;
  tourId?: string;
  /** Help-centre article to link as "Read the guide"; `null`/omitted hides it. */
  article?: HelpArticleId | null;
  className?: string;
}) {
  const { t } = useTranslation();
  const text = t("How it works");

  const guide = article ? (
    <button
      type="button"
      onClick={() => openHelpLink(helpArticleUrl(article))}
      aria-description={t("opens enode.ai")}
      className="clickable-small shrink-0 whitespace-nowrap text-foreground-muted underline underline-offset-2 hover:opacity-80"
    >
      {t("Read the guide")}
    </button>
  ) : null;

  if (placement === "inline") {
    return (
      <span data-tour={tourId} className="inline-flex items-center gap-3">
        <button
          type="button"
          onClick={onClick}
          className={`clickable-small inline-flex shrink-0 items-center gap-1.5 whitespace-nowrap text-foreground-muted transition-colors hover:text-foreground-default ${className ?? ""}`}
        >
          <InfoIcon className="size-4" />
          <span>{text}</span>
        </button>
        {guide}
      </span>
    );
  }

  return (
    <>
      {/* One inline-flex wrapper carries the tour anchor for both forms. */}
      <span data-tour={tourId} className="inline-flex shrink-0">
        {/* ≥sm: the head's secondary pill (wrapped: Button sets its own
            display, so the breakpoint swap lives on the wrapper). */}
        <span className="hidden sm:inline-flex">
          <Button
            variant="secondary"
            size="header"
            leadingIcon={<InfoIcon className="size-4" />}
            onClick={onClick}
            className={className}
          >
            {text}
          </Button>
        </span>
        {/* <sm: the head's icon circle. */}
        <HeaderIconButton
          label={text}
          icon={<InfoIcon className="size-5" />}
          onClick={onClick}
          className={`sm:hidden ${className ?? ""}`}
        />
      </span>
      {guide}
    </>
  );
}
