'use client';

import { motion, useReducedMotion, AnimatePresence } from 'framer-motion';
import { useEffect, useState } from 'react';
import { 
  UserPlus, 
  Link as LinkIcon, 
  Settings,
  Trophy,
  Car,
  Users,
  Shield,
  CheckCircle2
} from 'lucide-react';

interface WorkflowStep {
  id: number;
  icon: typeof UserPlus;
  title: string;
  description: string;
  color: string;
}

const WORKFLOW_STEPS: WorkflowStep[] = [
  {
    id: 1,
    icon: UserPlus,
    title: 'Create Account',
    description: 'Sign up with email or connect iRacing',
    color: 'text-primary-blue',
  },
  {
    id: 2,
    icon: LinkIcon,
    title: 'Link iRacing',
    description: 'Connect your iRacing profile for stats',
    color: 'text-purple-400',
  },
  {
    id: 3,
    icon: Settings,
    title: 'Configure Profile',
    description: 'Set up your racing preferences',
    color: 'text-warning-amber',
  },
  {
    id: 4,
    icon: Trophy,
    title: 'Join Leagues',
    description: 'Find and join competitive leagues',
    color: 'text-performance-green',
  },
  {
    id: 5,
    icon: Car,
    title: 'Start Racing',
    description: 'Compete and track your progress',
    color: 'text-primary-blue',
  },
];

export default function AuthWorkflowMockup() {
  const shouldReduceMotion = useReducedMotion();
  const [isMounted, setIsMounted] = useState(false);
  const [activeStep, setActiveStep] = useState(0);

  useEffect(() => {
    setIsMounted(true);
  }, []);

  useEffect(() => {
    if (!isMounted) return;
    
    const interval = setInterval(() => {
      setActiveStep((prev) => (prev + 1) % WORKFLOW_STEPS.length);
    }, 3000);

    return () => clearInterval(interval);
  }, [isMounted]);

  if (!isMounted) {
    return (
      <div className="relative w-full">
        <div className="bg-iron-gray/50 rounded-2xl border border-charcoal-outline p-6">
          <div className="flex justify-between gap-2">
            {WORKFLOW_STEPS.map((step) => (
              <div key={step.id} className="flex flex-col items-center text-center flex-1">
                <div className="w-10 h-10 rounded-lg bg-iron-gray border border-charcoal-outline flex items-center justify-center mb-2">
                  <step.icon className={`w-4 h-4 ${step.color}`} />
                </div>
                <h4 className="text-xs font-medium text-white">{step.title}</h4>
              </div>
            ))}
          </div>
        </div>
      </div>
    );
  }

  return (
    <div className="relative w-full">
      <div className="bg-iron-gray/50 rounded-2xl border border-charcoal-outline p-4 sm:p-6 overflow-hidden">
        {/* Connection Lines */}
        <div className="absolute top-[3.5rem] left-[8%] right-[8%] hidden sm:block">
          <div className="h-0.5 bg-charcoal-outline relative">
            <motion.div
              className="absolute h-full bg-gradient-to-r from-primary-blue to-performance-green"
              initial={{ width: '0%' }}
              animate={{ width: `${(activeStep / (WORKFLOW_STEPS.length - 1)) * 100}%` }}
              transition={{ duration: 0.5, ease: 'easeInOut' }}
            />
          </div>
        </div>

        {/* Steps */}
        <div className="flex justify-between gap-2 relative">
          {WORKFLOW_STEPS.map((step, index) => {
            const isActive = index === activeStep;
            const isCompleted = index < activeStep;
            const StepIcon = step.icon;

            return (
              <motion.div
                key={step.id}
                className="flex flex-col items-center text-center cursor-pointer flex-1"
                onClick={() => setActiveStep(index)}
                whileHover={{ scale: 1.05 }}
                whileTap={{ scale: 0.95 }}
              >
                <motion.div
                  className={`w-10 h-10 sm:w-12 sm:h-12 rounded-lg border flex items-center justify-center mb-2 transition-all duration-300 ${
                    isActive 
                      ? 'bg-primary-blue/20 border-primary-blue shadow-[0_0_15px_rgba(25,140,255,0.3)]' 
                      : isCompleted
                        ? 'bg-performance-green/20 border-performance-green/50'
                        : 'bg-iron-gray border-charcoal-outline'
                  }`}
                  animate={isActive && !shouldReduceMotion ? {
                    scale: [1, 1.08, 1],
                    transition: { duration: 1, repeat: Infinity }
                  } : {}}
                >
                  {isCompleted ? (
                    <CheckCircle2 className="w-4 h-4 sm:w-5 sm:h-5 text-performance-green" />
                  ) : (
                    <StepIcon className={`w-4 h-4 sm:w-5 sm:h-5 ${isActive ? step.color : 'text-gray-500'}`} />
                  )}
                </motion.div>
                <h4 className={`text-xs font-medium transition-colors hidden sm:block ${
                  isActive ? 'text-white' : 'text-gray-400'
                }`}>
                  {step.title}
                </h4>
              </motion.div>
            );
          })}
        </div>

        {/* Active Step Preview - Mobile */}
        <AnimatePresence mode="wait">
          <motion.div
            key={activeStep}
            initial={{ opacity: 0, y: 5 }}
            animate={{ opacity: 1, y: 0 }}
            exit={{ opacity: 0, y: -5 }}
            transition={{ duration: 0.2 }}
            className="mt-4 pt-4 border-t border-charcoal-outline sm:hidden"
          >
            <div className="text-center">
              <p className="text-xs text-gray-400 mb-1">
                Step {activeStep + 1}: {WORKFLOW_STEPS[activeStep].title}
              </p>
              <p className="text-xs text-gray-500">
                {WORKFLOW_STEPS[activeStep].description}
              </p>
            </div>
          </motion.div>
        </AnimatePresence>
      </div>
    </div>
  );
}