import React, { useState, useEffect } from 'react';
import { View, Text, TextInput, Button, StyleSheet } from 'react-native';
import { signOut } from 'firebase/auth';
import { doc, getDoc, updateDoc } from 'firebase/firestore';
import { auth, db } from '../config/firebase';

export default function ProfileScreen({ navigation }: any) {
  const [name, setName] = useState('');

  useEffect(() => {
    loadProfile();
  }, []);

  const loadProfile = async () => {
    const userDoc = await getDoc(doc(db, 'users', auth.currentUser!.uid));
    setName(userDoc.data()?.name || '');
  };

  const handleSave = async () => {
    await updateDoc(doc(db, 'users', auth.currentUser!.uid), { name });
    alert('Profil mentve');
  };

  const handleLogout = async () => {
    await signOut(auth);
    navigation.replace('Login');
  };

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Profil</Text>
      <TextInput style={styles.input} placeholder="Név" value={name} onChangeText={setName} />
      <Button title="Mentés" onPress={handleSave} />
      <Button title="Kijelentkezés" onPress={handleLogout} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, padding: 16, justifyContent: 'center' },
  title: { fontSize: 24, fontWeight: 'bold', marginBottom: 16, textAlign: 'center' },
  input: { borderWidth: 1, padding: 8, marginBottom: 16, borderRadius: 4 },
});
