import React, { useState } from 'react';
import { View, StyleSheet, Image, TouchableOpacity } from 'react-native';
import { colors, spacing } from '../../theme/tokens';
import { SafeScreen } from '../../components/layout/SafeScreen';
import { H2, Body, Button, Input, Caption } from '../../components/ui';
import { t } from '../../i18n';
import { useQuizStore } from '../../store';

interface AuthScreenProps {
  onAuth: () => void;
}

export function AuthScreen({ onAuth }: AuthScreenProps) {
  const [isSignIn, setIsSignIn] = useState(false);
  const [name, setName] = useState('');
  const [spouseName, setSpouseName] = useState('');
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');
  const { setPartnerAName, setPartnerBName } = useQuizStore();

  const handleSubmit = () => {
    if (name.trim()) setPartnerAName(name.trim());
    if (spouseName.trim()) setPartnerBName(spouseName.trim());
    onAuth();
  };

  return (
    <SafeScreen scroll>
      <View style={styles.container}>
        <View style={styles.logoSection}>
          <Image
            source={require('../../../assets/branding/logo-wifaq.png')}
            style={styles.logo}
            resizeMode="contain"
          />
        </View>

        <H2 align="center" style={styles.title}>
          {isSignIn ? t('auth.signIn') : t('auth.createAccount')}
        </H2>
        <Body align="center" style={styles.subtitle} color={colors.neutral[500]}>
          {t('auth.welcomeSubtitle')}
        </Body>

        <View style={styles.form}>
          {!isSignIn && (
            <>
              <Input
                label={t('auth.name')}
                value={name}
                onChangeText={setName}
                autoCapitalize="words"
              />
              <Input
                label={t('auth.spouseName')}
                value={spouseName}
                onChangeText={setSpouseName}
                autoCapitalize="words"
              />
            </>
          )}
          <Input
            label={t('auth.email')}
            value={email}
            onChangeText={setEmail}
            keyboardType="email-address"
            autoCapitalize="none"
          />
          <Input
            label={t('auth.password')}
            value={password}
            onChangeText={setPassword}
            secureTextEntry
          />

          <Button
            title={isSignIn ? t('auth.signIn') : t('auth.createAccount')}
            onPress={handleSubmit}
            fullWidth
            size="lg"
          />
        </View>

        <View style={styles.switchRow}>
          <Caption>
            {isSignIn ? "Pas encore de compte ?" : "Déjà un compte ?"}
          </Caption>
          <TouchableOpacity onPress={() => setIsSignIn(!isSignIn)}>
            <Caption style={styles.switchLink}>
              {isSignIn ? t('auth.createAccount') : t('auth.signIn')}
            </Caption>
          </TouchableOpacity>
        </View>
      </View>
    </SafeScreen>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: spacing['4xl'],
  },
  logoSection: {
    alignItems: 'center',
    marginBottom: spacing['2xl'],
  },
  logo: {
    width: 72,
    height: 72,
  },
  title: {
    marginBottom: spacing.sm,
  },
  subtitle: {
    marginBottom: spacing['3xl'],
    paddingHorizontal: spacing.xl,
  },
  form: {
    gap: spacing.xs,
  },
  switchRow: {
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    gap: spacing.xs,
    marginTop: spacing['2xl'],
    paddingBottom: spacing['4xl'],
  },
  switchLink: {
    color: colors.primary[500],
    fontWeight: '600',
  },
});
