// Sparkline SVG en pure code. Pas de lib charts. Reçoit un tableau de
// nombres et trace un polyline lissé sur un viewBox normalisé.

type Props = {
  data: number[];
  width?: number;
  height?: number;
  className?: string;
  fill?: boolean;
  strokeWidth?: number;
};

export function Sparkline({
  data,
  width = 120,
  height = 32,
  className = "",
  fill = true,
  strokeWidth = 1.5,
}: Props) {
  if (!data || data.length < 2) {
    return (
      <svg
        width={width}
        height={height}
        viewBox={`0 0 ${width} ${height}`}
        className={className}
        aria-hidden="true"
      />
    );
  }

  const max = Math.max(...data);
  const min = Math.min(...data);
  const range = max - min || 1;
  const stride = width / (data.length - 1);
  const padding = strokeWidth;

  const points = data.map((v, i) => {
    const x = i * stride;
    const y =
      height - padding - ((v - min) / range) * (height - 2 * padding);
    return [x, y] as const;
  });

  const polyline = points.map(([x, y]) => `${x.toFixed(1)},${y.toFixed(1)}`).join(" ");
  const area = `M0,${height} L${polyline.replace(/ /g, " L")} L${width},${height} Z`;

  return (
    <svg
      width={width}
      height={height}
      viewBox={`0 0 ${width} ${height}`}
      className={className}
      preserveAspectRatio="none"
      aria-hidden="true"
    >
      {fill && (
        <path d={area} fill="currentColor" opacity="0.1" />
      )}
      <polyline
        points={polyline}
        fill="none"
        stroke="currentColor"
        strokeWidth={strokeWidth}
        strokeLinejoin="round"
        strokeLinecap="round"
      />
    </svg>
  );
}
