"""
Task Planner - Generate multi-step plans to achieve goals
"""

class TaskPlanner:
    def __init__(self):
        self.available_tools = ["bash", "file_create", "file_read", "git"]
    
    def create_plan(self, goal_description):
        """Generate a multi-step plan for a goal"""
        plan = {
            "goal": goal_description,
            "steps": [],
            "estimated_time": 0
        }
        
        # Simple planning heuristics
        if "create" in goal_description.lower() and "file" in goal_description.lower():
            plan["steps"] = [
                {"step": 1, "action": "design_structure", "tool": None},
                {"step": 2, "action": "create_file", "tool": "file_create"},
                {"step": 3, "action": "verify_creation", "tool": "bash"}
            ]
            plan["estimated_time"] = 3
        
        elif "analyze" in goal_description.lower():
            plan["steps"] = [
                {"step": 1, "action": "read_files", "tool": "file_read"},
                {"step": 2, "action": "extract_patterns", "tool": None},
                {"step": 3, "action": "generate_report", "tool": "file_create"}
            ]
            plan["estimated_time"] = 5
        
        elif "git" in goal_description.lower() or "commit" in goal_description.lower():
            plan["steps"] = [
                {"step": 1, "action": "check_status", "tool": "git"},
                {"step": 2, "action": "add_files", "tool": "git"},
                {"step": 3, "action": "commit_changes", "tool": "git"}
            ]
            plan["estimated_time"] = 2
        
        else:
            plan["steps"] = [
                {"step": 1, "action": "analyze_goal", "tool": None},
                {"step": 2, "action": "execute_main_task", "tool": "bash"},
                {"step": 3, "action": "verify_completion", "tool": "bash"}
            ]
            plan["estimated_time"] = 4
        
        return plan
    
    def optimize_plan(self, plan, learned_knowledge=None):
        """Optimize plan based on learned knowledge"""
        if learned_knowledge and "best_approach" in learned_knowledge:
            for step in plan["steps"]:
                if step["action"] in learned_knowledge["best_approach"]:
                    step["optimized"] = True
        return plan
    
    def validate_plan(self, plan):
        """Check if plan is executable"""
        issues = []
        for step in plan["steps"]:
            if step["tool"] and step["tool"] not in self.available_tools:
                issues.append(f"Step {step['step']}: Tool '{step['tool']}' not available")
        return {"valid": len(issues) == 0, "issues": issues}

if __name__ == "__main__":
    print("=" * 70)
    print("TASK PLANNER TEST")
    print("=" * 70)
    planner = TaskPlanner()
    goal = "Create a configuration file for the system"
    plan = planner.create_plan(goal)
    print(f"\n🎯 Goal: {plan['goal']}")
    print(f"📋 Plan ({len(plan['steps'])} steps):")
    for step in plan['steps']:
        tool_str = f"[{step['tool']}]" if step['tool'] else ""
        print(f"   {step['step']}. {step['action']} {tool_str}")
    print(f"⏱️  Estimated time: {plan['estimated_time']} minutes")
    validation = planner.validate_plan(plan)
    print(f"\n✅ Plan valid: {validation['valid']}")
    print("\n✅ TASK PLANNER OPERATIONAL")
