"""
TaskManager
Generated by Eden via recursive self-improvement
2025-10-28 17:20:16.391245
"""

class Task:
    def __init__(self, title, description, due_date, priority):
        self.title = title
        self.description = description
        self.due_date = due_date
        self.priority = priority  # 1 (high), 2 (medium), 3 (low)
        self.status = "pending"
        self.creation_date = datetime.today().date()

    def __repr__(self):
        return f"Task(title={self.title}, status={self.status})"

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

    def add_task(self, title, description, due_date, priority):
        """
        Add a new task to the manager.
        
        Args:
            title (str): The title of the task
            description (str): Description of the task
            due_date (date): Due date of the task
            priority (int): Priority level (1=high, 2=medium, 3=low)
        """
        if any(task.title == title for task in self.tasks):
            raise ValueError("Task with this title already exists.")
            
        new_task = Task(title=title, description=description, due_date=due_date, priority=priority)
        self.tasks.append(new_task)
    
    def mark_done(self, title):
        """
        Mark a task as completed.
        
        Args:
            title (str): Title of the task to mark as done
        """
        for task in self.tasks:
            if task.title == title:
                task.status = "completed"
                return True
        return False  # Task not found

    def get_all_tasks(self):
        """Get all tasks with their details."""
        return [vars(task) for task in sorted(self.tasks, key=lambda x: x.creation_date)]

    def get_upcoming_tasks(self, days=7):
        """
        Get tasks due within the next X days.
        
        Args:
            days (int): Number of days to look ahead (default 7)
        """
        today = datetime.today().date()
        upcoming = []
        for task in self.tasks:
            if task.status == "completed":
                continue
            delta = (task.due_date - today).days
            if 0 <= delta <= days:
                upcoming.append(vars(task))
        return upcoming

    def check_overdue_and_high_priority(self):
        """
        Check for overdue or high priority pending tasks.
        Returns a list of tasks that are either:
        - Overdue (due date was in the past)
        - High priority (priority level 1) and still pending
        """
        today = datetime.today().date()
        important_tasks = []
        
        for task in self.tasks:
            if task.status == "completed":
                continue
                
            # Check for overdue tasks
            if task.due_date < today:
                important_tasks.append(vars(task))
                
            # Check for high priority pending tasks
            if task.priority == 1 and task.status == "pending":
                important_tasks.append(vars(task))
        
        return important_tasks

# Example usage:
if __name__ == "__main__":
    import datetime
    
    manager = TaskManager()
    
    # Add some tasks
    manager.add_task(
        title="Complete project",
        description="Finish the client project by end of day",
        due_date=datetime.date(2024, 3, 15),
        priority=1
    )
    
    manager.add_task(
        title="Buy groceries",
        description="Pick up milk and eggs from store",
        due_date=datetime.date(2024, 3, 14),
        priority=3
    )
    
    # Get all tasks
    all_tasks = manager.get_all_tasks()
    print("All tasks:")
    for task in all_tasks:
        print(f"{task['title']} - {task['status']}")
    
    # Mark a task as done
    manager.mark_done("Buy groceries")
    
    # Get upcoming tasks (next 7 days)
    upcoming = manager.get_upcoming_tasks()
    print("\nUpcoming tasks within next week:")
    for task in upcoming:
        print(f"{task['title']} - Due: {task['due_date']}")
    
    # Check for important tasks
    important = manager.check_overdue_and_high_priority()
    print("\nImportant tasks to focus on:")
    for task in important:
        print(f"{task['title']} - Priority: {task['priority']}, Status: {task['status']}")