import { useState } from 'react';
import { ActivityIndicator, Text, TouchableOpacity, View } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { useTranslation } from 'react-i18next';
import { useMailConnect } from '../../hooks/useMailConnect';
import { useColors } from '../../lib/theme';
import { humanizeMailError } from '../../lib/mailErrors';
import { FormSheet } from '../FormSheet';
import { SheetFieldStack } from '../SheetFieldStack';

type Props = {
  visible: boolean;
  email: string;
  onClose: () => void;
  onSuccess: () => void;
};

/**
 * Sheet zum Aktualisieren des App-Passworts eines bereits verbundenen Postfachs.
 * Nutzt POST /api/mail/connect (upsert) — Backend ersetzt verschlüsseltes Passwort.
 */
export function EditMailAccountSheet({ visible, email, onClose, onSuccess }: Props) {
  const { t } = useTranslation();
  const { connect, connecting, error: connectError } = useMailConnect();

  const [password, setPassword] = useState('');
  const [passwordVisible, setPasswordVisible] = useState(false);
  const [formError, setFormError] = useState<string | null>(null);
  const [fieldsComplete, setFieldsComplete] = useState(false);

  function handleClose() {
    setPassword('');
    setPasswordVisible(false);
    setFormError(null);
    setFieldsComplete(false);
    onClose();
  }

  async function handleSave() {
    setFormError(null);
    const result = await connect({ email, password });
    if (result.ok) {
      handleClose();
      onSuccess();
    } else {
      setFormError(result.error ?? t('mail.connect_failed'));
    }
  }

  return (
    <FormSheet
      visible={visible}
      onClose={handleClose}
      title={t('mail.edit_account_title')}
      initialHeightPct={0.5}
      growWithKeyboard
    >
      <SheetFieldStack
        fields={[
          {
            key: 'password',
            label: t('mail.form_password_label'),
            placeholder: t('mail.app_password_placeholder'),
            value: password,
            onChangeText: (v) => { setPassword(v); setFormError(null); },
            secureTextEntry: !passwordVisible,
            autoCapitalize: 'none',
            autoCorrect: false,
            validate: (v) =>
              v.trim().length === 0 ? t('mail.form_fields_required') : undefined,
            suffix: (
              <TouchableOpacity
                activeOpacity={0.7}
                onPress={() => setPasswordVisible((p) => !p)}
                hitSlop={8}
              >
                <Ionicons
                  name={passwordVisible ? 'eye-off-outline' : 'eye-outline'}
                  size={18}
                  color="#737373"
                />
              </TouchableOpacity>
            ),
          },
        ]}
        onComplete={() => setFieldsComplete(true)}
      >
        {/* Hint + Error + Save-Button */}
        <Text
          style={{
            fontSize: 13,
            fontFamily: 'Nunito_400Regular',
            color: '#6b7280',
            lineHeight: 18,
            marginBottom: 10,
          }}
        >
          {t('mail.edit_account_subtitle', { email })}
        </Text>

        {(formError ?? connectError) && (
          <View
            style={{
              backgroundColor: '#fef2f2',
              borderRadius: 10,
              padding: 12,
              flexDirection: 'row',
              gap: 8,
              alignItems: 'flex-start',
              marginBottom: 10,
            }}
          >
            <Ionicons name="alert-circle" size={16} color="#dc2626" style={{ marginTop: 1 }} />
            <Text
              style={{
                flex: 1,
                fontSize: 12,
                fontFamily: 'Nunito_400Regular',
                color: '#dc2626',
              }}
            >
              {formError ?? t(humanizeMailError(connectError))}
            </Text>
          </View>
        )}

        <TouchableOpacity
          activeOpacity={0.85}
          onPress={handleSave}
          disabled={connecting}
          style={{ marginTop: 4, marginBottom: 12 }}
        >
          <View
            style={{
              paddingVertical: 14,
              borderRadius: 12,
              backgroundColor: connecting ? '#bfdbfe' : '#007AFF',
              alignItems: 'center',
            }}
          >
            {connecting ? (
              <ActivityIndicator color="#fff" />
            ) : (
              <Text style={{ fontSize: 15, fontFamily: 'Nunito_700Bold', color: '#fff' }}>
                {t('mail.edit_account_save')}
              </Text>
            )}
          </View>
        </TouchableOpacity>
      </SheetFieldStack>
    </FormSheet>
  );
}
