'use client';

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

export function LeagueDiscoveryMockup() {
  const shouldReduceMotion = useReducedMotion();
  const [hoveredIndex, setHoveredIndex] = useState<number | null>(null);
  const [isMobile, setIsMobile] = useState(false);

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

  const leagues = [
    {
      name: 'Championship Series',
      carClass: 'GT3',
      region: 'EU',
      skill: '2.5k iRating',
      drivers: 48,
      schedule: 'Wed 20:00 CET',
      rating: 4.8,
      icon: '🏁'
    },
    {
      name: 'Touring Car Challenge',
      carClass: 'TCR',
      region: 'NA',
      skill: 'Open skill',
      drivers: 32,
      schedule: 'Sat 18:00 EST',
      rating: 4.6,
      icon: '🏎️'
    }
  ];

  if (isMobile) {
    return (
      <Box position="relative" fullWidth fullHeight bg="graphite-black" rounded="none" p={3} overflow="hidden">
        <Box mb={3}>
          <Box h="1.5" w="40" bg="white/10" rounded="none" mb={3} />
          <Box display="flex" gap={2} flexWrap="wrap">
            {['Game', 'Region'].map((filter) => (
              <Box
                key={filter}
                h="5"
                px={3}
                bg="panel-gray"
                border
                borderColor="primary-accent/30"
                rounded="none"
                display="flex"
                alignItems="center"
              >
                <Box h="1" w="8" bg="white/10" rounded="none" />
              </Box>
            ))}
          </Box>
        </Box>

        <Stack gap={2}>
          {leagues.map((league) => (
            <Box
              key={league.name}
              bg="panel-gray/40"
              border
              borderColor="border-gray/50"
              rounded="none"
              p={3}
            >
              <Box display="flex" alignItems="start" justifyContent="between" mb={2}>
                <Box display="flex" alignItems="center" gap={2}>
                  <Box h="10" w="10" rounded="none" border borderWidth="1px" borderColor="primary-accent/30" bg="graphite-black" display="flex" alignItems="center" justifyContent="center">
                    <Text size="xl">{league.icon}</Text>
                  </Box>
                  <Box>
                    <Box h="1.5" w="32" bg="white/10" rounded="none" mb={1.5} />
                    <Box display="flex" gap={1}>
                      <Box as="span" px={1.5} py={0.5} bg="primary-accent/10" border borderColor="primary-accent/20">
                        <Text size="xs" color="text-primary-accent" weight="bold" font="mono">{league.carClass}</Text>
                      </Box>
                    </Box>
                  </Box>
                </Box>
              </Box>

              <Box display="flex" alignItems="center" justifyContent="between">
                <Text size="xs" color="text-gray-500" font="mono">{league.drivers} DRIVERS • {league.schedule.toUpperCase()}</Text>
                <Box px={2} py={0.5} bg="primary-accent" rounded="none">
                  <Text size="xs" color="text-white" weight="bold">JOIN</Text>
                </Box>
              </Box>
            </Box>
          ))}
        </Stack>
      </Box>
    );
  }

  const containerVariants = {
    hidden: { opacity: 0 },
    visible: {
      opacity: 1,
      transition: { staggerChildren: shouldReduceMotion ? 0 : 0.15 }
    }
  };

  const cardVariants = {
    hidden: { opacity: 0, x: shouldReduceMotion ? 0 : -20 },
    visible: {
      opacity: 1,
      x: 0,
      transition: { duration: 0.4 }
    }
  };

  return (
    <Box position="relative" fullWidth fullHeight bg="graphite-black" rounded="none" p={{ base: 1.5, sm: 3, md: 5, lg: 8 }} overflow="hidden">
      <Box
        as={motion.div}
        initial={{ opacity: 0, y: shouldReduceMotion ? 0 : -10 }}
        animate={{ opacity: 1, y: 0 }}
        mb={{ base: 1.5, sm: 3, md: 4, lg: 6 }}
      >
        <Box h="2" w="48" bg="white/10" rounded="none" mb={4} />
        
        <Box display="flex" gap={2} flexWrap="wrap">
          {['Game', 'Region', 'Skill'].map((filter, i) => (
            <Box
              key={filter}
              as={motion.div}
              initial={{ opacity: 0, scale: shouldReduceMotion ? 1 : 0.9 }}
              animate={{ opacity: 1, scale: 1 }}
              transition={{ delay: shouldReduceMotion ? 0 : i * 0.1 }}
              h="6"
              px={4}
              bg="panel-gray"
              border
              borderColor="primary-accent/30"
              rounded="none"
              display="flex"
              alignItems="center"
            >
              <Box h="1" w="10" bg="white/10" rounded="none" />
            </Box>
          ))}
        </Box>
      </Box>

      <Box
        as={motion.div}
        variants={containerVariants}
        initial="hidden"
        animate="visible"
      >
        <Stack gap={2}>
          {leagues.map((league, index) => (
            <Box
              key={league.name}
              as={motion.div}
              variants={cardVariants}
              onHoverStart={() => !shouldReduceMotion && setHoveredIndex(index)}
              onHoverEnd={() => setHoveredIndex(null)}
              whileHover={shouldReduceMotion ? {} : {
                x: 4,
                borderColor: '#198CFF30',
                transition: { duration: 0.2 }
              }}
              bg="panel-gray/20"
              border
              borderColor="border-gray/50"
              rounded="none"
              p={{ base: 1.5, sm: 2, md: 3, lg: 4 }}
            >
              <Box display="flex" alignItems="start" justifyContent="between" mb={3}>
                <Box display="flex" alignItems="center" gap={3}>
                  <Box h={{ base: 7, sm: 8, md: 10, lg: 12 }} w={{ base: 7, sm: 8, md: 10, lg: 12 }} rounded="none" border borderWidth="1px" borderColor="primary-accent/30" bg="graphite-black" display="flex" alignItems="center" justifyContent="center" className="relative">
                    <Text size={{ base: 'base', sm: 'lg', md: 'xl', lg: '2xl' }}>{league.icon}</Text>
                    <Box position="absolute" top="-1px" left="-1px" w="2" h="2" borderTop borderLeft borderColor="primary-accent" />
                  </Box>
                  <Box>
                    <Box h="2" w="40" bg="white/10" rounded="none" mb={2} />
                    <Box display="flex" gap={2}>
                      <Box as="span" px={2} py={0.5} bg="primary-accent/10" border borderColor="primary-accent/20">
                        <Text 
                          // eslint-disable-next-line gridpilot-rules/component-classification
                          style={{ fontSize: '10px' }} 
                          color="text-primary-accent"
                          weight="bold"
                          font="mono"
                        >
                          {league.carClass}
                        </Text>
                      </Box>
                      <Box as="span" px={2} py={0.5} bg="telemetry-aqua/10" border borderColor="telemetry-aqua/20">
                        <Text 
                          // eslint-disable-next-line gridpilot-rules/component-classification
                          style={{ fontSize: '10px' }} 
                          color="text-telemetry-aqua"
                          weight="bold"
                          font="mono"
                        >
                          {league.region}
                        </Text>
                      </Box>
                    </Box>
                  </Box>
                </Box>
              </Box>

              <Box display="flex" alignItems="center" justifyContent="between">
                <Box display="flex" alignItems="center" gap={4}>
                  <Text 
                    // eslint-disable-next-line gridpilot-rules/component-classification
                    style={{ fontSize: '10px' }} 
                    color="text-gray-500"
                    font="mono"
                    weight="bold"
                  >
                    {league.drivers} DRIVERS
                  </Text>
                  <Text 
                    // eslint-disable-next-line gridpilot-rules/component-classification
                    style={{ fontSize: '10px' }} 
                    color="text-gray-500"
                    font="mono"
                    weight="bold"
                  >
                    {league.schedule.toUpperCase()}
                  </Text>
                </Box>
                
                <Box display="flex" gap={2}>
                  <Box
                    as={motion.button}
                    whileHover={shouldReduceMotion ? {} : { scale: 1.05 }}
                    px={3}
                    py={1}
                    bg="primary-accent"
                    color="text-white"
                    rounded="none"
                  >
                    <Text 
                      // eslint-disable-next-line gridpilot-rules/component-classification
                      style={{ fontSize: '10px' }}
                      weight="bold"
                    >
                      JOIN
                    </Text>
                  </Box>
                </Box>
              </Box>
            </Box>
          ))}
        </Stack>
      </Box>
    </Box>
  );
}
