import React, { useState, useCallback, useRef, useEffect } from 'react';
import {
  View,
  Text,
  StyleSheet,
  KeyboardAvoidingView,
  Platform,
  ScrollView,
  TouchableOpacity,
  TextInput,
} from 'react-native';
import { router, useLocalSearchParams } from 'expo-router';
import { Ionicons } from '@expo/vector-icons';
import { useAuth } from '@/contexts/AuthContext';
import { api } from '@/services/api';
import { Button } from '@/components/ui/Button';
import { ErrorMessage } from '@/components/ui/ErrorMessage';
import { AppColors, BorderRadius, FontSizes, Spacing } from '@/constants/theme';

const CODE_LENGTH = 6;

export default function VerifyOTPScreen() {
  const { email, isNewUser } = useLocalSearchParams<{ email: string; isNewUser: string }>();
  const { refreshAuth } = useAuth();

  const [code, setCode] = useState('');
  const [isLoading, setIsLoading] = useState(false);
  const [isResending, setIsResending] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [resendCountdown, setResendCountdown] = useState(0);

  const inputRef = useRef<TextInput>(null);

  // Focus input on mount
  useEffect(() => {
    setTimeout(() => {
      inputRef.current?.focus();
    }, 100);
  }, []);

  // Countdown timer for resend
  useEffect(() => {
    if (resendCountdown > 0) {
      const timer = setTimeout(() => setResendCountdown(resendCountdown - 1), 1000);
      return () => clearTimeout(timer);
    }
  }, [resendCountdown]);

  const handleCodeChange = (text: string) => {
    // Only allow digits
    const digits = text.replace(/\D/g, '').slice(0, CODE_LENGTH);
    setCode(digits);
    setError(null);

    // Auto-submit when code is complete
    if (digits.length === CODE_LENGTH) {
      handleVerify(digits);
    }
  };

  const handleVerify = useCallback(async (verifyCode?: string) => {
    const codeToVerify = verifyCode || code;

    if (codeToVerify.length !== CODE_LENGTH) {
      setError('Please enter the 6-digit code');
      return;
    }

    setIsLoading(true);
    setError(null);

    try {
      const response = await api.verifyOTP(email!, codeToVerify);

      if (response.ok && response.data) {
        // Refresh auth state
        await refreshAuth();

        // Check if new user needs to complete profile
        const user = response.data.user;
        if (!user.firstName || !user.lastName) {
          // New user - go to complete profile
          router.replace({
            pathname: '/(auth)/complete-profile',
            params: { email: email! },
          });
        } else {
          // Existing user - go to main app
          router.replace('/(tabs)');
        }
      } else {
        setError(response.error?.message || 'Invalid code. Please try again.');
        setCode('');
      }
    } catch (err) {
      setError('Something went wrong. Please try again.');
      setCode('');
    } finally {
      setIsLoading(false);
    }
  }, [code, email, refreshAuth]);

  const handleResend = useCallback(async () => {
    if (resendCountdown > 0) return;

    setIsResending(true);
    setError(null);

    try {
      const response = await api.requestOTP(email!);

      if (response.ok) {
        setResendCountdown(60); // 60 seconds cooldown
        setCode('');
      } else {
        setError(response.error?.message || 'Failed to resend code');
      }
    } catch (err) {
      setError('Failed to resend code. Please try again.');
    } finally {
      setIsResending(false);
    }
  }, [email, resendCountdown]);

  // Render code input boxes
  const renderCodeBoxes = () => {
    const boxes = [];
    for (let i = 0; i < CODE_LENGTH; i++) {
      const isActive = i === code.length;
      const isFilled = i < code.length;

      boxes.push(
        <View
          key={i}
          style={[
            styles.codeBox,
            isActive && styles.codeBoxActive,
            isFilled && styles.codeBoxFilled,
          ]}
        >
          <Text style={styles.codeDigit}>
            {code[i] || ''}
          </Text>
        </View>
      );
    }
    return boxes;
  };

  return (
    <KeyboardAvoidingView
      style={styles.container}
      behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
    >
      <ScrollView
        contentContainerStyle={styles.scrollContent}
        keyboardShouldPersistTaps="handled"
        showsVerticalScrollIndicator={false}
      >
        {/* Back Button */}
        <TouchableOpacity style={styles.backButton} onPress={() => router.back()}>
          <Ionicons name="arrow-back" size={24} color={AppColors.textPrimary} />
        </TouchableOpacity>

        {/* Header */}
        <View style={styles.header}>
          <View style={styles.iconContainer}>
            <Ionicons name="mail-open-outline" size={48} color={AppColors.primary} />
          </View>
          <Text style={styles.title}>Check your email</Text>
          <Text style={styles.subtitle}>
            We sent a verification code to
          </Text>
          <Text style={styles.email}>{email}</Text>
        </View>

        {/* Error */}
        {error && (
          <ErrorMessage
            message={error}
            onDismiss={() => setError(null)}
          />
        )}

        {/* Code Input */}
        <TouchableOpacity
          style={styles.codeContainer}
          activeOpacity={1}
          onPress={() => inputRef.current?.focus()}
        >
          {renderCodeBoxes()}
        </TouchableOpacity>

        {/* Hidden actual input */}
        <TextInput
          ref={inputRef}
          style={styles.hiddenInput}
          value={code}
          onChangeText={handleCodeChange}
          keyboardType="number-pad"
          maxLength={CODE_LENGTH}
          autoComplete="one-time-code"
          textContentType="oneTimeCode"
        />

        {/* Verify Button */}
        <Button
          title="Verify"
          onPress={() => handleVerify()}
          loading={isLoading}
          disabled={code.length !== CODE_LENGTH}
          fullWidth
          size="lg"
        />

        {/* Resend */}
        <View style={styles.resendContainer}>
          <Text style={styles.resendText}>Didn't receive the code? </Text>
          {resendCountdown > 0 ? (
            <Text style={styles.resendCountdown}>
              Resend in {resendCountdown}s
            </Text>
          ) : (
            <TouchableOpacity onPress={handleResend} disabled={isResending}>
              <Text style={[styles.resendLink, isResending && styles.resendLinkDisabled]}>
                {isResending ? 'Sending...' : 'Resend'}
              </Text>
            </TouchableOpacity>
          )}
        </View>

        {/* Change email */}
        <TouchableOpacity style={styles.changeEmailButton} onPress={() => router.back()}>
          <Text style={styles.changeEmailText}>Use a different email</Text>
        </TouchableOpacity>
      </ScrollView>
    </KeyboardAvoidingView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: AppColors.background,
  },
  scrollContent: {
    flexGrow: 1,
    paddingHorizontal: Spacing.lg,
    paddingTop: Spacing.xl,
    paddingBottom: Spacing.xl,
  },
  backButton: {
    width: 40,
    height: 40,
    borderRadius: BorderRadius.full,
    backgroundColor: AppColors.surface,
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: Spacing.lg,
  },
  header: {
    alignItems: 'center',
    marginBottom: Spacing.xl,
  },
  iconContainer: {
    width: 80,
    height: 80,
    borderRadius: BorderRadius.full,
    backgroundColor: `${AppColors.primary}15`,
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: Spacing.lg,
  },
  title: {
    fontSize: FontSizes['2xl'],
    fontWeight: '700',
    color: AppColors.textPrimary,
    marginBottom: Spacing.sm,
  },
  subtitle: {
    fontSize: FontSizes.base,
    color: AppColors.textSecondary,
  },
  email: {
    fontSize: FontSizes.base,
    fontWeight: '600',
    color: AppColors.textPrimary,
    marginTop: Spacing.xs,
  },
  codeContainer: {
    flexDirection: 'row',
    justifyContent: 'center',
    gap: Spacing.sm,
    marginBottom: Spacing.xl,
  },
  codeBox: {
    width: 48,
    height: 56,
    borderRadius: BorderRadius.md,
    borderWidth: 2,
    borderColor: AppColors.border,
    backgroundColor: AppColors.surface,
    justifyContent: 'center',
    alignItems: 'center',
  },
  codeBoxActive: {
    borderColor: AppColors.primary,
  },
  codeBoxFilled: {
    borderColor: AppColors.primary,
    backgroundColor: `${AppColors.primary}10`,
  },
  codeDigit: {
    fontSize: FontSizes['2xl'],
    fontWeight: '700',
    color: AppColors.textPrimary,
  },
  hiddenInput: {
    position: 'absolute',
    opacity: 0,
    height: 0,
    width: 0,
  },
  resendContainer: {
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    marginTop: Spacing.xl,
  },
  resendText: {
    fontSize: FontSizes.base,
    color: AppColors.textSecondary,
  },
  resendLink: {
    fontSize: FontSizes.base,
    color: AppColors.primary,
    fontWeight: '600',
  },
  resendLinkDisabled: {
    opacity: 0.5,
  },
  resendCountdown: {
    fontSize: FontSizes.base,
    color: AppColors.textMuted,
  },
  changeEmailButton: {
    alignItems: 'center',
    marginTop: Spacing.lg,
  },
  changeEmailText: {
    fontSize: FontSizes.sm,
    color: AppColors.textSecondary,
    textDecorationLine: 'underline',
  },
});
