"""
IntelligentTaskManager
Generated by Eden via recursive self-improvement
2025-10-28 08:54:36.165128
"""

class IntelligentTaskManager:
    """
    An intelligent task management system that understands natural language task descriptions.
    It analyzes the content of each task to determine its priority and urgency,
    allowing users to focus on the most important tasks first.

    Attributes:
        tasks (list): List of tuples containing tasks with their calculated priority levels
    """

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

    def add_task(self, task_description: str) -> None:
        """
        Adds a new task to the manager. Analyzes the task description to determine its priority.

        Args:
            task_description (str): The description of the task to be added
        """
        priority = self._calculate_priority(task_description)
        self.tasks.append((task_description, priority))

    def _calculate_priority(self, text: str) -> int:
        """
        Calculates a priority score for a given text based on keywords and length.

        Args:
            text (str): The text to analyze

        Returns:
            int: A priority score between 0 (low priority) and 5 (critical)
        """
        # Keywords that indicate high priority
        urgency_keywords = ['urgent', 'critical', 'priority', 'important']
        complexity_keywords = ['complex', 'difficult', 'multifaceted']

        points = 1  # Base priority

        # Check for urgency keywords
        for keyword in urgency_keywords:
            if keyword in text.lower():
                points += 2

        # Check for complexity keywords
        for keyword in complexity_keywords:
            if keyword in text.lower():
                points += 1

        # Add points based on length (longer tasks are more complex)
        length_score = min(len(text), 100) // 25
        points += length_score

        # Cap the maximum priority at 5 and minimum at 1
        return max(1, min(points, 5))

    def get_tasks_sorted(self) -> list:
        """
        Returns all tasks sorted by priority in descending order.

        Returns:
            list: List of tuples (task_description, priority)
        """
        # Sort tasks based on priority and insertion order for ties
        return sorted(self.tasks, key=lambda x: (-x[1], self.tasks.index(x)))

    def get_highest_priority_task(self) -> tuple or None:
        """
        Returns the task with the highest priority.

        Returns:
            tuple or None: Tuple of (task_description, priority) if tasks exist
        """
        if not self.tasks:
            return None
        # Find task with maximum priority
        max_priority = max(task[1] for task in self.tasks)
        candidates = [task for task in self.tasks if task[1] == max_priority]
        # Return the first candidate (oldest one if multiple have same max priority)
        return candidates[0]

# Example usage:
if __name__ == "__main__":
    manager = IntelligentTaskManager()

    # Add tasks
    manager.add_task("Buy groceries")
    manager.add_task("Call doctor - urgent")
    manager.add_task("Review Q3 reports thoroughly")
    manager.add_task("Fix Wi-Fi issue at home")
    manager.add_task("Plan surprise party")

    # Get all tasks sorted by priority
    sorted_tasks = manager.get_tasks_sorted()
    print("All tasks sorted by priority:")
    for task in sorted_tasks:
        print(f"Priority {task[1]}: {task[0]}")

    # Get highest priority task
    highest_priority = manager.get_highest_priority_task()
    if highest_priority:
        print("\nHighest priority task:", highest_priority)