import { HelpCircle } from 'lucide-react';

import { type ReactNode } from 'react';

import { Popover, PopoverContent, PopoverTrigger } from '@/Components/ui/popover';

interface ContextualHelpProps {
  children: ReactNode;
}

/**
 * Small (?) icon that shows a plain-English explanation popover on click.
 * Place next to metric labels or jargon terms.
 */
export default function ContextualHelp({ children }: ContextualHelpProps) {
  return (
    <Popover>
      <PopoverTrigger asChild>
        <button
          type="button"
          className="inline-flex items-center justify-center rounded-full text-muted-foreground/60 hover:text-muted-foreground transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
          aria-label="More info"
        >
          <HelpCircle className="h-3.5 w-3.5" />
        </button>
      </PopoverTrigger>
      <PopoverContent className="w-64 text-sm text-muted-foreground" side="top" align="start">
        {children}
      </PopoverContent>
    </Popover>
  );
}

// ── Spotlight Tour ────────────────────────────────────────────────────────────

interface SpotlightTipProps {
  /** 1-based current step number */
  step: number;
  totalSteps: number;
  title: string;
  description: string;
  /** Called when the user advances to the next step or clicks "Done" on the last step */
  onNext: () => void;
  /** Called when the user clicks "Skip tour" */
  onSkip: () => void;
  /** Whether this is the last step (renders "Done" instead of "Next →") */
  isLast: boolean;
}

/**
 * Fixed bottom-center tour step card for sequential product onboarding.
 * Used by RecommendationsFirstVisitTour. Each step explains a UI feature
 * without requiring real DOM-element spotlight refs.
 *
 * ACT-005: First-visit tour for the recommendations page.
 */
export function SpotlightTip({
  step,
  totalSteps,
  title,
  description,
  onNext,
  onSkip,
  isLast,
}: SpotlightTipProps) {
  return (
    <div
      role="dialog"
      aria-modal="false"
      aria-label={`Product tour: step ${step} of ${totalSteps}`}
      className="fixed bottom-6 left-1/2 z-50 w-[calc(100vw-2rem)] max-w-sm -translate-x-1/2 rounded-xl border border-border bg-card p-4 shadow-lg"
    >
      {/* Header row: step counter + skip */}
      <div className="mb-2 flex items-center justify-between">
        <span className="text-xs font-medium text-muted-foreground">
          Step {step} of {totalSteps}
        </span>
        <button
          type="button"
          onClick={onSkip}
          className="text-xs text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring rounded-sm"
          aria-label="Skip tour"
        >
          Skip tour
        </button>
      </div>

      {/* Content */}
      <p className="mb-1 text-sm font-semibold text-foreground">{title}</p>
      <p className="mb-4 text-sm leading-relaxed text-muted-foreground">{description}</p>

      {/* Footer: dot indicators + action button */}
      <div className="flex items-center justify-between">
        <div className="flex gap-1.5" aria-hidden="true">
          {Array.from({ length: totalSteps }, (_, i) => (
            <span
              key={i}
              className={`h-1.5 w-1.5 rounded-full transition-colors ${
                i + 1 === step ? 'bg-primary' : 'bg-muted-foreground/30'
              }`}
            />
          ))}
        </div>
        <button
          type="button"
          onClick={onNext}
          className="inline-flex items-center gap-1 rounded-lg bg-primary px-3 py-1.5 text-xs font-semibold text-primary-foreground transition-colors hover:bg-primary/90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
        >
          {isLast ? 'Done' : 'Next →'}
        </button>
      </div>
    </div>
  );
}
