import { useId } from 'react';
import { cn } from '@/lib/cn';

export interface ChakraPoint {
  key: string;
  name: string;
  value: number;
  color: string;
}

/** Vertical position of each chakra on the 200×260 figure, crown → root. */
const Y_POSITIONS = [24, 47, 79, 112, 140, 166, 192];

/**
 * Glowing meditating silhouette with the seven chakra points.
 * `points` must be ordered crown → root.
 */
export function ChakraFigure({ points, className }: { points: ChakraPoint[]; className?: string }) {
  const id = useId();

  return (
    <svg viewBox="0 0 200 260" className={cn('overflow-visible', className)} aria-hidden>
      <defs>
        <radialGradient id={`${id}-body`} cx="50%" cy="45%" r="60%">
          <stop offset="0%" stopColor="#B4C6FF" stopOpacity="0.8" />
          <stop offset="60%" stopColor="#6D7CFF" stopOpacity="0.4" />
          <stop offset="100%" stopColor="#5B6CFF" stopOpacity="0.06" />
        </radialGradient>
        <radialGradient id={`${id}-halo`} cx="50%" cy="50%" r="50%">
          <stop offset="0%" stopColor="#7C8CFF" stopOpacity="0.35" />
          <stop offset="100%" stopColor="#7C8CFF" stopOpacity="0" />
        </radialGradient>
        <filter id={`${id}-glow`} x="-50%" y="-50%" width="200%" height="200%">
          <feGaussianBlur stdDeviation="3.5" />
        </filter>
      </defs>

      <ellipse cx="100" cy="128" rx="96" ry="128" fill={`url(#${id}-halo)`} />

      <g stroke="#C9D4FF" strokeOpacity="0.55" strokeWidth="1" fill={`url(#${id}-body)`}>
        <circle cx="100" cy="44" r="21" />
        <path d="M90 64 L90 76 C72 79 61 87 59 104 C55 132 51 160 45 184 C36 191 26 199 28 213 C30 228 62 236 100 236 C138 236 170 228 172 213 C174 199 164 191 155 184 C149 160 145 132 141 104 C139 87 128 79 110 76 L110 64 Z" />
      </g>
      <g stroke="#C9D4FF" strokeOpacity="0.28" strokeWidth="1" fill="none">
        <path d="M68 110 C64 142 70 170 90 196" />
        <path d="M132 110 C136 142 130 170 110 196" />
        <path d="M44 212 C70 202 130 202 156 212" />
      </g>

      {points.map((p, i) => (
        <g key={p.key}>
          <circle cx="100" cy={Y_POSITIONS[i]} r="7" fill={p.color} filter={`url(#${id}-glow)`} />
          <circle cx="100" cy={Y_POSITIONS[i]} r="4" fill={p.color} />
          <circle cx="100" cy={Y_POSITIONS[i]} r="1.6" fill="#fff" fillOpacity="0.9" />
        </g>
      ))}
    </svg>
  );
}

export function ChakraBars({ points, className }: { points: ChakraPoint[]; className?: string }) {
  return (
    <ul className={cn('space-y-3', className)}>
      {points.map((p) => (
        <li key={p.key}>
          <div className="flex items-center justify-between gap-3 text-[11px]">
            <span className="flex items-center gap-2 text-white/80">
              <span className="size-1.5 rounded-full" style={{ backgroundColor: p.color }} />
              {p.name}
            </span>
            <span className="text-white/50 tabular-nums">{p.value}%</span>
          </div>
          <div className="mt-1.5 h-[3px] rounded-full bg-white/10">
            <div
              className="h-full rounded-full"
              style={{ width: `${p.value}%`, backgroundColor: p.color, boxShadow: `0 0 8px ${p.color}` }}
            />
          </div>
        </li>
      ))}
    </ul>
  );
}
