'use client';

import { motion, useReducedMotion } from 'framer-motion';
import { useState, useEffect } from 'react';
import { Stack } from '@/ui/Stack';
import { Text } from '@/ui/Text';
import { Heading } from '@/ui/Heading';

export function RaceHistoryMockup() {
  const shouldReduceMotion = useReducedMotion();
  const [isMobile, setIsMobile] = useState(false);

  useEffect(() => {
    const checkMobile = () => setIsMobile(window.innerWidth < 768);
    checkMobile();
    window.addEventListener('resize', checkMobile);
    return () => window.removeEventListener('resize', checkMobile);
  }, []);

  const itemVariants = {
    hidden: { opacity: 0, scale: shouldReduceMotion ? 1 : 0.95 },
    visible: { opacity: 1, scale: 1 }
  };

  // Simple, elegant mobile version - just the core story
  if (isMobile) {
    return (
      <Stack position="relative" fullWidth fullHeight bg="graphite-black" rounded="none" p={4} overflow="hidden" display="flex" alignItems="center" justifyContent="center">
        <Stack
          as={motion.div}
          initial="hidden"
          animate="visible"
          variants={{
            visible: { transition: { staggerChildren: 0.12 } }
          }}
          w="full"
          maxWidth="xs"
        >
          <Stack gap={4}>
            {/* Race result - clean and simple */}
            <Stack as={motion.div} variants={itemVariants}>
              <Stack bg="panel-gray/60" rounded="none" p={4} border borderWidth="1px" borderColor="primary-accent/40">
                <Stack display="flex" alignItems="center" gap={3}>
                  <Stack h="14" w="14" bg="primary-accent/10" rounded="none" display="flex" alignItems="center" justifyContent="center" flexShrink={0} border borderColor="primary-accent/30">
                    <Text size="2xl" weight="bold" color="text-primary-accent" font="mono">P3</Text>
                  </Stack>
                  <Stack>
                    <Text size="base" weight="bold" color="text-white" block className="uppercase tracking-widest">Watkins Glen</Text>
                    <Text size="xs" color="text-gray-500" block font="mono">GT3 SPRINT</Text>
                  </Stack>
                </Stack>
              </Stack>
            </Stack>

            {/* Simple arrow */}
            <Stack as={motion.div} variants={itemVariants} display="flex" justifyContent="center">
              <Text color="text-primary-accent" size="2xl">↓</Text>
            </Stack>

            {/* Updates - minimal */}
            <Stack as={motion.div} variants={itemVariants}>
              <Stack bg="panel-gray/40" rounded="none" p={3} border borderColor="border-gray/50">
                <Stack gap={2}>
                  <Text 
                    // eslint-disable-next-line gridpilot-rules/component-classification
                    style={{ fontSize: '10px' }} 
                    color="text-gray-500" 
                    textAlign="center" 
                    mb={2} 
                    block
                    font="mono"
                    uppercase
                    className="tracking-widest"
                  >
                    Profile Updated
                  </Text>
                  <Stack display="flex" gap={2}>
                    <Stack flexGrow={1} bg="graphite-black" rounded="none" py={2} textAlign="center" border borderColor="primary-accent/30">
                      <Text size="xs" color="text-primary-accent" weight="bold" block font="mono">STATS ↑</Text>
                    </Stack>
                    <Stack flexGrow={1} bg="graphite-black" rounded="none" py={2} textAlign="center" border borderColor="success-green/30">
                      <Text size="xs" color="text-success-green" weight="bold" block font="mono">+12</Text>
                    </Stack>
                  </Stack>
                </Stack>
              </Stack>
            </Stack>
          </Stack>
        </Stack>
      </Stack>
    );
  }

  // Desktop version - richer with more updates
  return (
    <Stack position="relative" fullWidth fullHeight bg="graphite-black" rounded="none" p={{ base: 1.5, sm: 3, md: 5, lg: 8 }} overflow="hidden">
      <Stack
        as={motion.div}
        initial="hidden"
        animate="visible"
        variants={{
          visible: { transition: { staggerChildren: shouldReduceMotion ? 0 : 0.12 } }
        }}
      >
        <Stack gap={{ base: 2, sm: 3, md: 4, lg: 5 }}>
          {/* Race Result Card - Enhanced */}
          <Stack as={motion.div} variants={itemVariants}>
            <Stack bg="panel-gray/40" rounded="none" p={{ base: 2, sm: 3, md: 4, lg: 5 }} border borderWidth="1px" borderColor="primary-accent/40">
              <Stack display="flex" alignItems="center" gap={{ base: 2, sm: 3, md: 4 }}>
                <Stack position="relative" h={{ base: 12, sm: 14, md: 16, lg: 20 }} w={{ base: 12, sm: 14, md: 16, lg: 20 }} bg="graphite-black" rounded="none" border borderWidth="1px" borderColor="primary-accent/30" overflow="hidden" flexShrink={0}>
                  <Stack position="absolute" inset="0" bg="gradient-to-br from-primary-accent/10 to-success-green/10" />
                  <Stack position="absolute" inset="0" display="flex" alignItems="center" justifyContent="center">
                    <Text color="text-white" size={{ base: 'xl', sm: '2xl', md: '3xl', lg: '4xl' }} weight="bold" font="mono">P3</Text>
                  </Stack>
                </Stack>
                <Stack flexGrow={1} minWidth="0">
                  <Stack display="flex" alignItems="center" gap={{ base: 1, sm: 1.5, md: 2 }} mb={1}>
                    <Stack w="1" h="4" bg="primary-accent" />
                    <Heading level={3} fontSize={{ base: 'sm', sm: 'base', md: 'lg', lg: 'xl' }} weight="bold" color="text-white" truncate className="uppercase tracking-widest">Watkins Glen</Heading>
                  </Stack>
                  <Text size={{ base: 'xs', sm: 'sm', md: 'base' }} color="text-gray-500" mb={1} block font="mono">GT3 SPRINT RACE</Text>
                  <Stack display="flex" alignItems="center" gap={{ base: 2, sm: 3, md: 4 }}>
                    <Text 
                      // eslint-disable-next-line gridpilot-rules/component-classification
                      style={{ fontSize: '10px' }} 
                      color="text-gray-600" 
                      font="mono"
                    >
                      24 DRIVERS
                    </Text>
                    <Text 
                      // eslint-disable-next-line gridpilot-rules/component-classification
                      style={{ fontSize: '10px' }} 
                      color="text-gray-600" 
                    >
                      •
                    </Text>
                    <Text 
                      // eslint-disable-next-line gridpilot-rules/component-classification
                      style={{ fontSize: '10px' }} 
                      color="text-gray-600" 
                      font="mono"
                    >
                      45 MIN
                    </Text>
                  </Stack>
                </Stack>
              </Stack>
            </Stack>
          </Stack>

          {/* Connection Flow with Animation */}
          <Stack as={motion.div} variants={itemVariants} display="flex" justifyContent="center">
            <Stack
              as={motion.div}
              display="flex"
              flexDirection="col"
              alignItems="center"
              gap={1}
              animate={shouldReduceMotion ? {} : {
                y: [0, 5, 0]
              }}
              transition={{ duration: 2, repeat: Infinity }}
            >
              <Text color="text-primary-accent" size={{ base: '2xl', sm: '3xl', md: '4xl' }}>↓</Text>
              <Text 
                // eslint-disable-next-line gridpilot-rules/component-classification
                style={{ fontSize: '10px' }} 
                color="text-primary-accent" 
                weight="bold"
                className="uppercase tracking-widest"
              >
                Auto-sync
              </Text>
            </Stack>
          </Stack>

          {/* Profile Updates Grid - More detailed */}
          <Stack as={motion.div} variants={itemVariants}>
            <Stack bg="panel-gray/20" rounded="none" p={{ base: 2, sm: 3, md: 4, lg: 5 }} border borderColor="border-gray/50">
              <Text size={{ base: 'xs', sm: 'sm', md: 'base' }} weight="bold" color="text-gray-500" textAlign="center" mb={{ base: 2, sm: 3, md: 4 }} block className="uppercase tracking-[0.2em]">
                Profile Updates
              </Text>
              
              <Stack display="grid" gridCols={2} gap={{ base: 2, sm: 3, md: 4 }}>
                {/* Career Stats Update */}
                <Stack bg="graphite-black" rounded="none" p={{ base: 2, sm: 3, md: 4 }} border borderColor="primary-accent/30">
                  <Stack display="flex" alignItems="center" justifyContent="between" mb={2}>
                    <Text 
                      // eslint-disable-next-line gridpilot-rules/component-classification
                      style={{ fontSize: '10px' }} 
                      color="text-gray-500" 
                      weight="bold"
                      className="uppercase tracking-widest"
                    >
                      Career Stats
                    </Text>
                    <Stack
                      as={motion.span}
                      color="text-success-green"
                      weight="bold"
                      // eslint-disable-next-line gridpilot-rules/component-classification
                      style={{ fontSize: '10px' }}
                      animate={shouldReduceMotion ? {} : { scale: [1, 1.2, 1] }}
                      transition={{ duration: 0.5, delay: 0.5 }}
                    >
                      ↑
                    </Stack>
                  </Stack>
                  <Text 
                    // eslint-disable-next-line gridpilot-rules/component-classification
                    style={{ fontSize: '9px' }} 
                    color="text-white" 
                    opacity={0.5} 
                    block
                    font="mono"
                  >
                    WINS: 24 → 25
                  </Text>
                </Stack>

                {/* Rating Update */}
                <Stack bg="graphite-black" rounded="none" p={{ base: 2, sm: 3, md: 4 }} border borderColor="success-green/30">
                  <Stack display="flex" alignItems="center" justifyContent="between" mb={2}>
                    <Text 
                      // eslint-disable-next-line gridpilot-rules/component-classification
                      style={{ fontSize: '10px' }} 
                      color="text-gray-500" 
                      weight="bold"
                      className="uppercase tracking-widest"
                    >
                      Rating
                    </Text>
                    <Stack
                      as={motion.span}
                      color="text-success-green"
                      weight="bold"
                      font="mono"
                      // eslint-disable-next-line gridpilot-rules/component-classification
                      style={{ fontSize: '10px' }}
                      animate={shouldReduceMotion ? {} : { scale: [1, 1.2, 1] }}
                      transition={{ duration: 0.5, delay: 0.6 }}
                    >
                      +12
                    </Stack>
                  </Stack>
                  <Text 
                    // eslint-disable-next-line gridpilot-rules/component-classification
                    style={{ fontSize: '9px' }} 
                    color="text-white" 
                    opacity={0.5} 
                    block
                    font="mono"
                  >
                    1342 → 1354
                  </Text>
                </Stack>

                {/* Season Points Update */}
                <Stack bg="graphite-black" rounded="none" p={{ base: 2, sm: 3, md: 4 }} border borderColor="warning-amber/30">
                  <Stack display="flex" alignItems="center" justifyContent="between" mb={2}>
                    <Text 
                      // eslint-disable-next-line gridpilot-rules/component-classification
                      style={{ fontSize: '10px' }} 
                      color="text-gray-500" 
                      weight="bold"
                      className="uppercase tracking-widest"
                    >
                      Season
                    </Text>
                    <Stack
                      as={motion.span}
                      color="text-warning-amber"
                      weight="bold"
                      font="mono"
                      // eslint-disable-next-line gridpilot-rules/component-classification
                      style={{ fontSize: '10px' }}
                      animate={shouldReduceMotion ? {} : { scale: [1, 1.2, 1] }}
                      transition={{ duration: 0.5, delay: 0.7 }}
                    >
                      +18
                    </Stack>
                  </Stack>
                  <Text 
                    // eslint-disable-next-line gridpilot-rules/component-classification
                    style={{ fontSize: '9px' }} 
                    color="text-white" 
                    opacity={0.5} 
                    block
                    font="mono"
                  >
                    248 → 266 PTS
                  </Text>
                </Stack>

                {/* Team Points Update */}
                <Stack bg="graphite-black" rounded="none" p={{ base: 2, sm: 3, md: 4 }} border borderColor="telemetry-aqua/30">
                  <Stack display="flex" alignItems="center" justifyContent="between" mb={2}>
                    <Text 
                      // eslint-disable-next-line gridpilot-rules/component-classification
                      style={{ fontSize: '10px' }} 
                      color="text-gray-500" 
                      weight="bold"
                      className="uppercase tracking-widest"
                    >
                      Team
                    </Text>
                    <Stack
                      as={motion.span}
                      color="text-telemetry-aqua"
                      weight="bold"
                      font="mono"
                      // eslint-disable-next-line gridpilot-rules/component-classification
                      style={{ fontSize: '10px' }}
                      animate={shouldReduceMotion ? {} : { scale: [1, 1.2, 1] }}
                      transition={{ duration: 0.5, delay: 0.8 }}
                    >
                      +18
                    </Stack>
                  </Stack>
                  <Text 
                    // eslint-disable-next-line gridpilot-rules/component-classification
                    style={{ fontSize: '9px' }} 
                    color="text-white" 
                    opacity={0.5} 
                    block
                    font="mono"
                  >
                    CONTRIBUTING
                  </Text>
                </Stack>
              </Stack>
            </Stack>
          </Stack>
        </Stack>
      </Stack>
    </Stack>
  );
}
