import React, { useState } from 'react';
import {
  View,
  Text,
  TextInput,
  TouchableOpacity,
  ScrollView,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useRouter } from 'expo-router';
import { useTheme } from '@/constants/ThemeContext';
import { ArrowLeft, Lock, Eye, EyeOff } from 'lucide-react-native';
import SecurityIllustration from '@/components/SecurityIllustration';
import styles from '@/styles/auth/reset-password.styles';

export default function ResetPasswordScreen() {
  const router = useRouter();
  const { isDark } = useTheme();

  const [password, setPassword] = useState('');
  const [confirmPassword, setConfirmPassword] = useState('');
  const [showPassword, setShowPassword] = useState(false);
  const [showConfirmPassword, setShowConfirmPassword] = useState(false);

  const getPasswordStrength = (pwd: string) => {
    if (!pwd) return { label: 'Password strength fair', color: '#38BDF8', ratio: 0.5 };
    let score = 0;
    if (pwd.length >= 6) score += 1;
    if (pwd.length >= 8) score += 1;
    if (/[0-9]/.test(pwd)) score += 1;
    if (/[^A-Za-z0-9]/.test(pwd) || (/[a-z]/.test(pwd) && /[A-Z]/.test(pwd))) score += 1;

    if (score <= 1) return { label: 'Password strength weak', color: '#EF4444', ratio: 0.25 };
    if (score === 2) return { label: 'Password strength fair', color: '#F59E0B', ratio: 0.5 };
    if (score === 3) return { label: 'Password strength good', color: '#38BDF8', ratio: 0.75 };
    return { label: 'Password strength strong', color: '#22C55E', ratio: 1.0 };
  };

  const strength = getPasswordStrength(password);

  const handleUpdatePassword = () => {
    router.push('/(auth)/password-updated' as any);
  };

  const handleBackToLogin = () => {
    router.push('/(auth)/login' as any);
  };

  return (
    <SafeAreaView style={[styles.safeArea, { backgroundColor: isDark ? '#050E24' : '#FFFFFF' }]}>
      <ScrollView
        contentContainerStyle={styles.scrollContent}
        showsVerticalScrollIndicator={false}
        keyboardShouldPersistTaps="handled"
      >
        {/* Top Back Navigation Header */}
        <TouchableOpacity
          onPress={() => router.back()}
          style={styles.backButton}
          activeOpacity={0.7}
        >
          <ArrowLeft size={20} color={isDark ? '#FFFFFF' : '#0C1C3E'} />
          <Text style={[styles.backText, { color: isDark ? '#FFFFFF' : '#0C1C3E' }]}>
            Back
          </Text>
        </TouchableOpacity>

        {/* Security Shield Hero Icon */}
        <View style={styles.heroContainer}>
          <SecurityIllustration size={160} />
        </View>

        {/* Header Title & Subtitle */}
        <View style={styles.headerContainer}>
          <Text style={[styles.title, { color: isDark ? '#FFFFFF' : '#0C1C3E' }]}>
            Reset Password
          </Text>
          <Text style={[styles.subtitle, { color: isDark ? '#94A3B8' : '#64748B' }]}>
            Your new password must be different from previous
          </Text>
        </View>

        {/* Form Inputs */}
        <View style={styles.formContainer}>
          {/* New Password */}
          <View style={styles.inputGroup}>
            <View
              style={[
                styles.inputWrapper,
                {
                  backgroundColor: isDark ? '#0F172A' : '#FFFFFF',
                  borderColor: isDark ? '#1E293B' : '#CBD5E1',
                },
              ]}
            >
              <Lock size={18} color={isDark ? '#94A3B8' : '#64748B'} style={{ marginRight: 10 }} />
              <View style={styles.inputInnerFlex}>
                <Text style={[styles.inputLabelInside, { color: isDark ? '#E2E8F0' : '#0F172A' }]}>
                  New password
                </Text>
                <TextInput
                  value={password}
                  onChangeText={setPassword}
                  placeholder="Enter New Password"
                  placeholderTextColor={isDark ? '#64748B' : '#94A3B8'}
                  secureTextEntry={!showPassword}
                  style={[styles.input, { color: isDark ? '#FFFFFF' : '#0F172A' }]}
                />
              </View>
              <TouchableOpacity onPress={() => setShowPassword(!showPassword)}>
                {showPassword ? (
                  <EyeOff size={20} color={isDark ? '#94A3B8' : '#64748B'} />
                ) : (
                  <Eye size={20} color={isDark ? '#94A3B8' : '#64748B'} />
                )}
              </TouchableOpacity>
            </View>
          </View>

          {/* Confirm Password */}
          <View style={styles.inputGroup}>
            <View
              style={[
                styles.inputWrapper,
                {
                  backgroundColor: isDark ? '#0F172A' : '#FFFFFF',
                  borderColor: isDark ? '#1E293B' : '#CBD5E1',
                },
              ]}
            >
              <Lock size={18} color={isDark ? '#94A3B8' : '#64748B'} style={{ marginRight: 10 }} />
              <View style={styles.inputInnerFlex}>
                <Text style={[styles.inputLabelInside, { color: isDark ? '#E2E8F0' : '#0F172A' }]}>
                  Confirm password
                </Text>
                <TextInput
                  value={confirmPassword}
                  onChangeText={setConfirmPassword}
                  placeholder="Confirm new password"
                  placeholderTextColor={isDark ? '#64748B' : '#94A3B8'}
                  secureTextEntry={!showConfirmPassword}
                  style={[styles.input, { color: isDark ? '#FFFFFF' : '#0F172A' }]}
                />
              </View>
              <TouchableOpacity onPress={() => setShowConfirmPassword(!showConfirmPassword)}>
                {showConfirmPassword ? (
                  <EyeOff size={20} color={isDark ? '#94A3B8' : '#64748B'} />
                ) : (
                  <Eye size={20} color={isDark ? '#94A3B8' : '#64748B'} />
                )}
              </TouchableOpacity>
            </View>
          </View>

          {/* Password Strength Indicator */}
          <View style={styles.strengthContainer}>
            <View style={styles.strengthTrack}>
              <View
                style={[
                  styles.strengthFill,
                  { width: `${strength.ratio * 100}%`, backgroundColor: strength.color },
                ]}
              />
            </View>
            <Text style={[styles.strengthText, { color: isDark ? '#CBD5E1' : '#334155' }]}>
              {strength.label}
            </Text>
          </View>

          {/* Update Password Button */}
          <TouchableOpacity
            style={styles.updateButton}
            onPress={handleUpdatePassword}
            activeOpacity={0.88}
          >
            <Text style={styles.updateButtonText}>Update Password</Text>
          </TouchableOpacity>

          {/* Back to Login Link */}
          <TouchableOpacity onPress={handleBackToLogin} style={styles.backToLoginContainer} activeOpacity={0.7}>
            <Text style={styles.backToLoginText}>Back to Login</Text>
          </TouchableOpacity>
        </View>
      </ScrollView>
    </SafeAreaView>
  );
}
