import React from 'react';
import { View, FlatList, StyleSheet } from 'react-native';
import { useNavigation } from '@react-navigation/native';
import { StackNavigationProp } from '@react-navigation/stack';
import { useTasks } from '../context/TasksContext';
import TaskCard from '../components/TaskCard';

type RootStackParamList = {
  TaskList: undefined;
  TaskEdit: { taskId: string };
};

type TaskListNavigationProp = StackNavigationProp<RootStackParamList, 'TaskList'>;

export default function TaskListScreen() {
  const { tasks } = useTasks();
  const navigation = useNavigation<TaskListNavigationProp>();

  const handlePressTask = (taskId: string) => {
    navigation.navigate('TaskEdit', { taskId });
  };

  return (
    <View style={styles.container}>
      <FlatList
        data={tasks}
        renderItem={({ item }) => <TaskCard task={item} onPress={handlePressTask} />}
        keyExtractor={item => item.id}
        numColumns={2}
        contentContainerStyle={styles.list}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  list: {
      padding: 8,
  }
});
