'use client';

import type { CSSProperties } from 'react';
import { cn } from '@/lib/utils/cn';
import type { PipelineStage } from '../api.client';
import { requiredNames, stageColor, stageStyle } from '../stageRules';

/**
 * The Leads design's stage stepper (components/lead-pipeline.js `stepper`, `.lpl-steps`): the
 * pipeline's stages in order in their colours, the current one with "In stage: Nd", the stages
 * before it done, and how many fields a stage requires. Other stages are buttons that move the
 * record there (when it may be edited).
 */
export function StageStepper({
  stages,
  currentId,
  days,
  label,
  lead,
  interactive,
  busy,
  onSelect,
}: {
  stages: PipelineStage[];
  currentId: string | null;
  /** Days in the current stage; none: no "In stage" chip. */
  days: number | null;
  /** "Lead stage". */
  label: string;
  /** Leads' default colours by stage name. */
  lead?: boolean;
  interactive: boolean;
  busy?: boolean;
  onSelect: (stage: PipelineStage) => void;
}) {
  const live = stages.filter((s) => !s.archived).sort((a, b) => a.displayOrder - b.displayOrder);
  const idx = live.findIndex((s) => s.id === currentId);
  const closed = live[idx]?.stageType === 'lost';
  return (
    <ol className="lpl-steps" aria-label={label} aria-busy={busy || undefined}>
      {live.map((s, i) => {
        const on = i === idx;
        const done = !closed && idx > -1 && i < idx && s.stageType !== 'lost';
        const req = requiredNames(s).length;
        const reqText = req ? `, requires ${req} field${req === 1 ? '' : 's'}` : '';
        const name = `${s.name}${on ? ' (current stage)' : ''}${reqText}`;
        const inner = (
          <>
            <span className="lpl-step__dot" aria-hidden="true" />
            <span className="lpl-step__name">{s.name}</span>
            {on && days !== null ? <span className="lpl-step__days">In stage: {days}d</span> : null}
            {req && !on ? (
              <span
                className="lpl-step__req"
                title={`Requires ${req} field${req === 1 ? '' : 's'}`}
              >
                {req} required
              </span>
            ) : null}
          </>
        );
        return (
          <li
            key={s.id}
            className={cn('lpl-step', on && 'is-current', done && 'is-done')}
            style={stageStyle(stageColor(s, { lead })) as CSSProperties}
          >
            {interactive && !on ? (
              <button
                type="button"
                className="lpl-step__btn"
                aria-label={`Move to ${name}`}
                disabled={busy}
                onClick={() => onSelect(s)}
              >
                {inner}
              </button>
            ) : (
              <span
                className="lpl-step__btn"
                aria-current={on ? 'step' : undefined}
                aria-label={name}
              >
                {inner}
              </span>
            )}
          </li>
        );
      })}
    </ol>
  );
}
