"use client";

import { CircleHelp } from "lucide-react";

import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import { cn } from "@/lib/utils";
import { dashboardExcludeNotes } from "@/app/customer/_components/dashboard-card-exclude-hint";
import type { AumCalcMode } from "@/app/customer/[tenant]/settings/_lib/aum-mode-api";
import type { MacroDashboardAssetType } from "@/app/customer/_lib/dashboard-layout-card-groups";

/** Compact ? control that keeps exclude / display-only notes off the figure line. */
export function DashboardFigureHint({
  content,
  label = "More about this figure",
  className,
}: {
  content: string | readonly string[];
  label?: string;
  className?: string;
}) {
  const lines = (Array.isArray(content) ? content : [content]).filter(Boolean);
  if (lines.length === 0) return null;

  return (
    <TooltipProvider delayDuration={200}>
      <Tooltip>
        <TooltipTrigger asChild>
          <button
            type="button"
            className={cn(
              "inline-flex size-3.5 shrink-0 items-center justify-center rounded-full text-muted-foreground transition-colors hover:text-foreground",
              className,
            )}
            aria-label={label}
            onClick={(event) => event.stopPropagation()}
          >
            <CircleHelp className="size-3" aria-hidden />
          </button>
        </TooltipTrigger>
        <TooltipContent side="top" className="max-w-[260px] text-xs leading-relaxed">
          <div className="space-y-0.5">
            {lines.map((line) => (
              <p key={line}>{line}</p>
            ))}
          </div>
        </TooltipContent>
      </Tooltip>
    </TooltipProvider>
  );
}

/**
 * Tip copy for a Macro type row — same amber span lines as the Default Cash/Leverage cards:
 * Net → `*excluding N transactions`
 * Gross → that line plus `Excl. Cash/Leverage: …` when a deduction exists.
 */
export function macroTypeRowHint(
  row: Pick<
    MacroDashboardAssetType,
    "plain" | "aumKey" | "label" | "excludeCount" | "grossDeductionFormatted"
  >,
  aumMode: AumCalcMode | undefined,
  figuresMasked: boolean,
): string[] | null {
  // `plain` means "don't fold into the Macro sum" (Overdraft, Accumulator/Options counts).
  // Only Overdraft gets the display-only tip — count cards must not show a ?.
  if (row.plain) {
    const label = row.label.trim().toLowerCase();
    if (label === "overdraft" || label === "od") {
      return ["Overdraft is for display only — it is not counted anywhere."];
    }
    return null;
  }
  if (!row.aumKey || aumMode == null) return null;

  // Same amber spans as Default Cash/Leverage:
  // Net → `*excluding N transactions`
  // Gross → that line + `Excl. Cash/Leverage: …` when a deduction exists.
  const deductionLabel =
    row.aumKey === "cash" ? "Cash" : row.aumKey === "leverage" ? "Leverage" : row.label;
  const notes = dashboardExcludeNotes({
    excludeCount: row.excludeCount,
    deductionLabel,
    deductionFormatted: aumMode === "gross" ? row.grossDeductionFormatted : null,
    figuresMasked,
  });
  const lines: string[] = [];
  if (!(Number(row.excludeCount) > 0)) {
    lines.push("No excluded transactions.");
  }
  lines.push(...notes);
  return lines.length > 0 ? lines : null;
}
