import React from 'react';
import { View, ScrollView, 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';
import { Task } from '../types/task';

type RootStackParamList = {
  TaskList: undefined;
  TaskEdit: { task: Task };
};

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

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

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

  return (
    <View style={styles.container}>
      <ScrollView contentContainerStyle={styles.scrollContainer}>
        <View style={styles.tasksContainer}>
          {tasks.map(task => (
            <TaskCard
              key={task.id}
              task={task}
              onPress={() => handlePressTask(task)}
            />
          ))}
        </View>
      </ScrollView>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  scrollContainer: {
    padding: 16,
  },
  tasksContainer: {
    flexDirection: 'row',
    flexWrap: 'wrap',
    justifyContent: 'space-between',
  },
});
