"""
SmartTaskManager
Generated by Eden via recursive self-improvement
2025-10-27 18:11:16.966201
"""

from datetime import date

class TodoList:
    """
    An intelligent todo list manager that allows adding, removing, and prioritizing tasks based on due dates and importance.
    
    Attributes:
        tasks (list): List of tasks, each represented as a dictionary with keys 'name', 
                     'description', 'priority', and 'due_date'.
    """

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

    def add_task(self, name, description="", priority="medium", due_date=None):
        """
        Add a new task to the todo list.
        
        Args:
            name (str): The name of the task.
            description (str, optional): Description of the task. Defaults to "".
            priority (str, optional): Priority level: 'high', 'medium', or 'low'. 
                                      Defaults to "medium".
            due_date (date, optional): Due date of the task as a date object. Defaults to None.
        """
        task = {
            "name": name,
            "description": description,
            "priority": priority,
            "due_date": due_date
        }
        self.tasks.append(task)

    def remove_task(self, name):
        """
        Remove a task by its name.
        
        Args:
            name (str): The name of the task to remove.
        """
        for i, task in enumerate(self.tasks):
            if task["name"] == name:
                del self.tasks[i]
                break

    def sort_tasks(self):
        """
        Sort tasks first by priority (highest first), then by due date (earliest first).
        """
        # Define a custom sorting key
        def sort_key(task):
            priority_map = {'high': 2, 'medium': 1, 'low': 0}
            return (-priority_map[task['priority']], task['due_date'])
        
        self.tasks.sort(key=sort_key)

    def list_tasks(self):
        """
        Return a string representation of all tasks.
        """
        output = []
        for task in self.tasks:
            priority = task['priority'].upper()
            description = task['description'] if task['description'] else ""
            due_date = task['due_date'] if task['due_date'] else "No due date"
            output.append(f"[{task['name']}]: {description} (Priority: {priority}, Due: {due_date})")
        return "\n".join(output)

    def get_due_soon(self):
        """
        Return tasks that are due within the next 7 days.
        
        Returns:
            list: List of tasks with due dates within the next week.
        """
        today = date.today()
        upcoming_tasks = []
        for task in self.tasks:
            if task['due_date']:
                delta = (task['due_date'] - today).days
                if 0 <= delta <= 7:
                    upcoming_tasks.append(task)
        return upcoming_tasks

# Example usage:
if __name__ == "__main__":
    todo = TodoList()
    
    # Add tasks
    todo.add_task("Complete project proposal", 
                  "Prepare final slides and documentation", 
                  "high", date(2023, 10, 30))
    todo.add_task("Team meeting", 
                  "Discuss Q4 goals", 
                  "medium", date(2023, 10, 25))
    todo.add_task("Buy groceries",
                  due_date=date(2023, 10, 28))
    todo.add_task("Review code changes",
                  priority="low")
    
    # Sort tasks
    todo.sort_tasks()
    
    print("All tasks:")
    print(todo.list_tasks())
    
    print("\nTasks due soon:")
    for task in todo.get_due_soon():
        print(f"-> {task['name']}: Due on {task['due_date']}")