'use client';

import { motion, useReducedMotion, useMotionValue, useSpring } from 'framer-motion';
import { useEffect, useState } from 'react';

export default function StandingsTableMockup() {
  const shouldReduceMotion = useReducedMotion();
  const [hoveredRow, setHoveredRow] = useState<number | null>(null);

  const getRowAnimation = (i: number) => ({
    hidden: { opacity: 0, y: shouldReduceMotion ? 0 : 10 },
    visible: {
      opacity: 1,
      y: 0,
      transition: {
        delay: shouldReduceMotion ? 0 : i * 0.05,
        type: 'spring' as const,
        stiffness: 300,
        damping: 24
      }
    }
  });

  return (
    <div className="relative w-full h-full bg-gradient-to-br from-deep-graphite via-iron-gray to-deep-graphite rounded-lg p-6 overflow-hidden">
      <div className="mb-4">
        <div className="flex items-center gap-4 pb-3 border-b border-charcoal-outline">
          <div className="text-xs font-mono text-gray-400">#</div>
          <div className="text-xs flex-1 font-semibold text-white">Driver</div>
          <div className="text-xs font-mono text-gray-400">Wins</div>
          <div className="text-xs font-mono text-gray-400">Points</div>
        </div>
      </div>

      <div className="space-y-1">
        {[1, 2, 3, 4, 5, 6, 7, 8].map((i) => (
          <motion.div
            key={i}
            variants={getRowAnimation(i)}
            initial="hidden"
            animate="visible"
            className={`relative flex items-center gap-4 py-3 px-3 rounded-lg border transition-all duration-150 ${
              i <= 3
                ? 'bg-gradient-to-r from-performance-green/10 to-iron-gray border-performance-green/20'
                : 'bg-iron-gray border-charcoal-outline'
            }`}
            onHoverStart={() => !shouldReduceMotion && setHoveredRow(i)}
            onHoverEnd={() => setHoveredRow(null)}
            whileHover={shouldReduceMotion ? {} : {
              scale: 1.01,
              boxShadow: '0 0 20px rgba(25,140,255,0.3)',
              transition: { duration: 0.15 }
            }}
          >
            <motion.div
              className={`h-7 w-7 rounded-full flex items-center justify-center font-semibold text-xs ${
                i <= 3
                  ? 'bg-primary-blue text-white shadow-glow'
                  : 'bg-charcoal-outline text-gray-400'
              }`}
              animate={
                shouldReduceMotion ? {} : i <= 3 && hoveredRow === i
                  ? { scale: 1.15, boxShadow: '0 0 28px rgba(25,140,255,0.5)' }
                  : {}
              }
            >
              {i}
            </motion.div>
            <div className="flex-1">
              <div className="h-3 w-full max-w-[140px] bg-white/10 rounded"></div>
            </div>
            <div className="h-3 w-16 bg-white/5 rounded font-mono"></div>
            <div className="relative">
              <AnimatedPoints
                points={300 - i * 20}
                position={i}
                shouldReduceMotion={shouldReduceMotion ?? false}
              />
            </div>
          </motion.div>
        ))}
      </div>
    </div>
  );
}

function AnimatedPoints({
  points,
  position,
  shouldReduceMotion
}: {
  points: number;
  position: number;
  shouldReduceMotion: boolean;
}) {
  const motionValue = useMotionValue(0);
  const spring = useSpring(motionValue, { stiffness: 50, damping: 20 });

  useEffect(() => {
    if (shouldReduceMotion) {
      motionValue.set(points);
    } else {
      setTimeout(() => motionValue.set(points), 100 + position * 50);
    }
  }, [points, position, shouldReduceMotion, motionValue]);

  const percentage = (points / 300) * 100;

  return (
    <div className="relative w-24 h-7 bg-charcoal-outline rounded border border-primary-blue/20 overflow-hidden">
      <motion.div
        className={`absolute inset-y-0 left-0 ${
          position <= 3
            ? 'bg-gradient-to-r from-performance-green/40 to-performance-green/20'
            : 'bg-gradient-to-r from-iron-gray to-charcoal-outline'
        }`}
        initial={{ width: '0%' }}
        animate={{ width: `${percentage}%` }}
        transition={{ duration: shouldReduceMotion ? 0 : 0.8, ease: 'easeOut', delay: 0.1 + position * 0.05 }}
      />
      <div className="relative h-full flex items-center justify-center">
        <motion.span className="text-xs font-mono font-semibold text-white">
          {shouldReduceMotion ? points : <motion.span>{spring}</motion.span>}
        </motion.span>
      </div>
    </div>
  );
}