import React, { useEffect, useState } from 'react';
import { View, Text, FlatList, StyleSheet } from 'react-native';
import { apiService } from '../services/api';

export default function UsersScreen({ filter }: { filter: string }) {
  const [users, setUsers] = useState([]);

  useEffect(() => {
    apiService.getUsers().then(setUsers);
  }, []);

  const filtered = users.filter((u: any) => u.name.toLowerCase().includes(filter.toLowerCase()));

  return (
    <View style={styles.container}>
      <FlatList
        data={filtered}
        keyExtractor={(item: any) => item.id.toString()}
        renderItem={({ item }: any) => (
          <View style={styles.item}>
            <Text style={styles.name}>{item.name}</Text>
            <Text>{item.email}</Text>
          </View>
        )}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, padding: 16 },
  item: { padding: 16, borderBottomWidth: 1 },
  name: { fontWeight: 'bold' },
});
