"""
TaskManager
Generated by Eden via recursive self-improvement
2025-10-28 05:46:11.257788
"""

from datetime import datetime

class TaskManager:
    """
    Manages a collection of tasks with operations to add, update, retrieve,
    and filter tasks based on priority and due date.
    
    Attributes:
        tasks (list): A list of dictionaries where each dictionary represents a task.
            Each task has an ID, title, priority, description, and due date.
    """

    def __init__(self):
        self.tasks = []

    def add_task(self, title: str, priority: int, description: str, due_date: str) -> str:
        """
        Adds a new task to the list of tasks.
        
        Args:
            title: The title of the task.
            priority: An integer representing the priority (1 for high, 2 for medium, 3 for low).
            description: Description of the task.
            due_date: The due date in 'YYYY-MM-DD' format.
            
        Returns:
            str: The ID of the newly added task.
        """
        task_id = len(self.tasks) + 1
        new_task = {
            "id": task_id,
            "title": title,
            "priority": priority,
            "description": description,
            "due_date": datetime.fromisoformat(due_date)
        }
        self.tasks.append(new_task)
        return f"Task {task_id} added."

    def update_task(self, task_id: int, new_title: str = None, 
                   new_priority: int = None, new_description: str = None,
                   new_due_date: str = None) -> str:
        """
        Updates an existing task by its ID with new information.
        
        Args:
            task_id: The ID of the task to update.
            new_title: New title (optional).
            new_priority: New priority (optional).
            new_description: New description (optional).
            new_due_date: New due date in 'YYYY-MM-DD' format (optional).
            
        Returns:
            str: Confirmation message if successful. Raises ValueError if task not found.
        """
        for task in self.tasks:
            if task["id"] == task_id:
                if new_title is not None:
                    task["title"] = new_title
                if new_priority is not None:
                    task["priority"] = new_priority
                if new_description is not None:
                    task["description"] = new_description
                if new_due_date is not None:
                    task["due_date"] = datetime.fromisoformat(new_due_date)
                return f"Task {task_id} updated successfully."
        raise ValueError(f"Task with ID {task_id} does not exist.")

    def get_all_tasks(self) -> list:
        """
        Returns all tasks in the system.
        
        Returns:
            list: A list of task dictionaries.
        """
        return self.tasks.copy()

    def filter_tasks(self, priority: int = None, due_date: str = None) -> list:
        """
        Filters tasks based on priority and/or due date.
        
        Args:
            priority: Integer representing the priority (optional).
            due_date: Due date in 'YYYY-MM-DD' format (optional).
            
        Returns:
            list: A filtered list of tasks.
        """
        filtered = []
        for task in self.tasks:
            if (priority is None or task["priority"] == priority) and \
               (due_date is None or str(task["due_date"]) == due_date):
                filtered.append(task)
        return filtered

    def count_overdue_tasks(self) -> int:
        """
        Counts the number of tasks that are overdue.
        
        Returns:
            int: The number of overdue tasks.
        """
        today = datetime.today()
        count = 0
        for task in self.tasks:
            if task["due_date"] < today:
                count += 1
        return count

# Example usage:
if __name__ == "__main__":
    tm = TaskManager()

    # Add new tasks
    print(tm.add_task("Complete Project", 1, "Finish the final deliverable.", "2023-12-31"))
    print(tm.add_task("Review Docs", 2, "Go over documentation changes.", "2024-01-05"))
    print(tm.add_task("Team Meeting", 1, "Daily stand-up at 9 AM.", "2024-01-04"))

    # Update a task
    print(tm.update_task(3, new_priority=1, new_due_date="2024-01-03"))

    # Retrieve and filter tasks
    all_tasks = tm.get_all_tasks()
    print("\nAll tasks:")
    for task in all_tasks:
        print(task)

    high_priority = tm.filter_tasks(priority=1)
    print("\nHigh priority tasks:")
    for task in high_priority:
        print(task)

    overdue = tm.count_overdue_tasks()
    print(f"\nNumber of overdue tasks: {overdue}")