import React from 'react';
import {
  View,
  Text,
  Image,
  TouchableOpacity,
  StyleSheet,
  Dimensions,
} from 'react-native';
import { useRouter } from 'expo-router';
import { Bus } from 'lucide-react-native';
import { useTheme } from '@/constants/ThemeContext';
import { useLanguage } from '@/i18n/LanguageContext';
import { markOnboardingCompleted } from '@/services/storage/onboardingStorage';
import images from '@/constants/images';
import { FacebookIcon, GoogleIcon, AppleIcon } from '@/components/SocialIcons';

const { width: SCREEN_WIDTH } = Dimensions.get('window');

interface WelcomeScreenProps {
  onGetStarted?: () => void;
  onLogin?: () => void;
  onRegister?: () => void;
}

export function WelcomeScreen({ onGetStarted, onLogin, onRegister }: WelcomeScreenProps) {
  const router = useRouter();
  const { isDark } = useTheme();
  const { t } = useLanguage();

  const handleRegister = () => {
    markOnboardingCompleted();
    if (onRegister) onRegister();
    else router.push('/(auth)/register' as any);
  };

  const handleLogin = () => {
    markOnboardingCompleted();
    if (onLogin) onLogin();
    else router.push('/(auth)/login' as any);
  };

  return (
    <View style={[styles.container, { backgroundColor: isDark ? '#071126' : '#F8FAFC' }]}>
      {/* Top Rounded Arc Header */}
      <View style={[styles.headerWrapper, { backgroundColor: isDark ? '#050E24' : '#0B1F4D' }]}>
        {/* Subtle concentric inner arc shadow ring */}
        <View style={styles.innerArcRing} />
        {/* Centered Circular Badge */}
        <View style={[styles.iconCircleBadge, { backgroundColor: isDark ? '#0B1F4D' : '#FFFFFF' }]}>
          <Bus size={32} color={isDark ? '#38BDF8' : '#0284C7'} />
        </View>
      </View>

      {/* Hero Bus Illustration */}
      <View style={styles.heroImageWrapper}>
        <Image
          source={images.onboardingBusTravel}
          style={styles.busImage}
          resizeMode="contain"
        />
      </View>

      {/* Welcome Titles */}
      <View style={styles.titleContainer}>
        <Text style={[styles.welcomeTitle, { color: isDark ? '#FFFFFF' : '#0B1F4D' }]}>
          Welcome to TravelCam
        </Text>
        <Text style={[styles.subtitle, { color: isDark ? '#38BDF8' : '#0284C7' }]}>
          Book smart, travel safely, enjoy your trip
        </Text>
      </View>

      {/* Floating Action Card */}
      <View
        style={[
          styles.cardContainer,
          {
            backgroundColor: isDark ? '#0F172A' : '#FFFFFF',
            borderColor: isDark ? '#1E293B' : '#E2E8F0',
          },
        ]}
      >
        {/* Primary Create Account Button */}
        <TouchableOpacity
          style={styles.primaryButton}
          onPress={handleRegister}
          activeOpacity={0.88}
        >
          <Text style={styles.primaryButtonText}>{t('register') || 'Create an account'}</Text>
        </TouchableOpacity>

        {/* Secondary Login Button */}
        <TouchableOpacity
          style={[styles.secondaryButton, { backgroundColor: isDark ? '#1E293B' : '#0B1F4D' }]}
          onPress={handleLogin}
          activeOpacity={0.88}
        >
          <Text style={styles.secondaryButtonText}>{t('login') || 'Login'}</Text>
        </TouchableOpacity>

        {/* Divider */}
        <View style={styles.dividerRow}>
          <View style={[styles.dividerLine, { backgroundColor: isDark ? '#334155' : '#E2E8F0' }]} />
          <Text style={[styles.dividerText, { color: isDark ? '#94A3B8' : '#64748B' }]}>
            Or connect using
          </Text>
          <View style={[styles.dividerLine, { backgroundColor: isDark ? '#334155' : '#E2E8F0' }]} />
        </View>

        {/* Social Connections Row */}
        <View style={styles.socialRow}>
          <TouchableOpacity
            style={[
              styles.socialButton,
              {
                backgroundColor: isDark ? '#1E293B' : '#F8FAFC',
                borderColor: isDark ? '#334155' : '#E2E8F0',
              },
            ]}
            onPress={handleLogin}
            activeOpacity={0.8}
          >
            <FacebookIcon />
          </TouchableOpacity>
          <TouchableOpacity
            style={[
              styles.socialButton,
              {
                backgroundColor: isDark ? '#1E293B' : '#F8FAFC',
                borderColor: isDark ? '#334155' : '#E2E8F0',
              },
            ]}
            onPress={handleLogin}
            activeOpacity={0.8}
          >
            <GoogleIcon />
          </TouchableOpacity>
          <TouchableOpacity
            style={[
              styles.socialButton,
              {
                backgroundColor: isDark ? '#1E293B' : '#F8FAFC',
                borderColor: isDark ? '#334155' : '#E2E8F0',
              },
            ]}
            onPress={handleLogin}
            activeOpacity={0.8}
          >
            <AppleIcon color={isDark ? '#FFFFFF' : '#0B1F4D'} />
          </TouchableOpacity>
        </View>
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    width: SCREEN_WIDTH,
    alignItems: 'center',
    justifyContent: 'space-between',
    paddingBottom: 20,
    overflow: 'hidden',
  },
  headerWrapper: {
    width: '100%',
    height: 140,
    borderBottomLeftRadius: SCREEN_WIDTH * 0.4,
    borderBottomRightRadius: SCREEN_WIDTH * 0.4,
    alignItems: 'center',
    justifyContent: 'center',
    position: 'relative',
    overflow: 'hidden',
  },
  innerArcRing: {
    position: 'absolute',
    top: -30,
    width: '92%',
    height: 120,
    borderRadius: SCREEN_WIDTH * 0.35,
    backgroundColor: 'rgba(255, 255, 255, 0.05)',
  },
  iconCircleBadge: {
    width: 64,
    height: 64,
    borderRadius: 32,
    justifyContent: 'center',
    alignItems: 'center',
    shadowColor: '#0B1F4D',
    shadowOffset: { width: 0, height: 4 },
    shadowOpacity: 0.15,
    shadowRadius: 8,
    elevation: 6,
    zIndex: 20,
  },
  heroImageWrapper: {
    width: '100%',
    height: 135,
    alignItems: 'center',
    justifyContent: 'center',
    marginTop: 4,
    marginBottom: 4,
  },
  busImage: {
    width: '85%',
    height: '100%',
  },
  titleContainer: {
    alignItems: 'center',
    marginVertical: 4,
    paddingHorizontal: 20,
  },
  welcomeTitle: {
    fontSize: 24,
    fontWeight: '900',
    textAlign: 'center',
    letterSpacing: -0.5,
  },
  subtitle: {
    fontSize: 13,
    fontWeight: '700',
    marginTop: 4,
    textAlign: 'center',
  },
  cardContainer: {
    width: SCREEN_WIDTH * 0.9,
    borderRadius: 24,
    borderWidth: 1.5,
    paddingHorizontal: 20,
    paddingVertical: 20,
    marginBottom: 8,
    shadowColor: '#0B1F4D',
    shadowOffset: { width: 0, height: 8 },
    shadowOpacity: 0.08,
    shadowRadius: 16,
    elevation: 6,
  },
  primaryButton: {
    backgroundColor: '#0284C7',
    borderRadius: 14,
    paddingVertical: 14,
    alignItems: 'center',
    marginBottom: 10,
    shadowColor: '#0284C7',
    shadowOffset: { width: 0, height: 4 },
    shadowOpacity: 0.25,
    shadowRadius: 8,
    elevation: 3,
  },
  primaryButtonText: {
    color: '#FFFFFF',
    fontSize: 15,
    fontWeight: '800',
  },
  secondaryButton: {
    borderRadius: 14,
    paddingVertical: 14,
    alignItems: 'center',
    marginBottom: 14,
  },
  secondaryButtonText: {
    color: '#FFFFFF',
    fontSize: 15,
    fontWeight: '800',
  },
  dividerRow: {
    flexDirection: 'row',
    alignItems: 'center',
    marginVertical: 10,
  },
  dividerLine: {
    flex: 1,
    height: 1,
  },
  dividerText: {
    fontSize: 11,
    fontWeight: '600',
    marginHorizontal: 10,
  },
  socialRow: {
    flexDirection: 'row',
    justifyContent: 'center',
    gap: 16,
    marginTop: 4,
  },
  socialButton: {
    width: 54,
    height: 40,
    borderRadius: 12,
    borderWidth: 1.5,
    justifyContent: 'center',
    alignItems: 'center',
  },
});