// components/StatsBar.tsx
"use client";

import { useEffect, useRef, useState } from "react";

const stats = [
  { value: 30, suffix: "+", label: "Jahre IT-Erfahrung", accent: "orange" },
  { value: 99, suffix: "%", label: "Server-Uptime", accent: "blue" },
  { value: 50, suffix: "+", label: "Abgeschlossene Projekte", accent: "orange" },
  { value: 24, suffix: "h", label: "Support-Reaktionszeit", accent: "blue" },
];

function Counter({ value, suffix, accent }: { value: number; suffix: string; accent: string }) {
  const [count, setCount] = useState(0);
  const ref = useRef<HTMLSpanElement>(null);
  const started = useRef(false);

  useEffect(() => {
    const el = ref.current;
    if (!el) return;

    const observer = new IntersectionObserver(
      ([entry]) => {
        if (entry.isIntersecting && !started.current) {
          started.current = true;
          const duration = 1500;
          const start = performance.now();

          const tick = (now: number) => {
            const progress = Math.min((now - start) / duration, 1);
            const eased = 1 - Math.pow(1 - progress, 3);
            setCount(Math.floor(eased * value));
            if (progress < 1) requestAnimationFrame(tick);
          };

          requestAnimationFrame(tick);
          observer.disconnect();
        }
      },
      { threshold: 0.3 }
    );

    observer.observe(el);
    return () => observer.disconnect();
  }, [value]);

  const color = accent === "orange" ? "text-orange-400" : "text-blue-400";

  return (
    <span ref={ref} className={`text-4xl sm:text-5xl font-black ${color}`}>
      {count}{suffix}
    </span>
  );
}

export default function StatsBar() {
  return (
    <section className="py-12 px-4 sm:px-6 lg:px-8 relative">
      <div className="absolute inset-0 bg-[#111925]" />
      <div className="max-w-7xl mx-auto relative">
        <div className="grid grid-cols-2 lg:grid-cols-4 gap-8">
          {stats.map((stat) => (
            <div key={stat.label} className="text-center">
              <Counter value={stat.value} suffix={stat.suffix} accent={stat.accent} />
              <p className="text-slate-500 text-sm mt-1">{stat.label}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}
