#!/usr/bin/env python3
"""
Custom orchestrator example - Replace the default Contractor.

Use this when you want complete control over the workflow,
like implementing your own SDLC process.
"""

from agentchain import (
    AgentChain,
    Orchestrator,
    Agent,
    AgentContext,
    AgentResult,
    orchestrator,
)


@orchestrator(name="my_orchestrator", role="Custom Project Manager")
class MyOrchestrator(Orchestrator):
    """
    Custom orchestrator that implements a different workflow.
    
    This example uses a waterfall-style approach:
    1. Plan everything first
    2. Implement all tasks
    3. Test everything
    4. Generate documentation
    """
    
    def execute(self, context: AgentContext) -> AgentResult:
        requirements = context.get("requirements", "")
        
        self._logger.info("=== Starting Custom Workflow ===")
        
        # Phase 1: Planning
        self._logger.info("Phase 1: Planning")
        planner = self.get_agent("planner")
        if planner:
            plan_result = self.invoke_agent("planner", AgentContext(
                task="Create plan",
                inputs={"requirements": requirements}
            ))
            if not plan_result.success:
                return plan_result
        
        # Phase 2: Implementation (you decide the order!)
        self._logger.info("Phase 2: Implementation")
        coder = self.get_agent("coder")
        if coder:
            # Implement in your custom order
            pass
        
        # Phase 3: Testing (if tester agent registered)
        self._logger.info("Phase 3: Testing")
        tester = self.get_agent("tester")
        if tester:
            self.invoke_agent("tester", context)
        
        # Phase 4: Documentation
        self._logger.info("Phase 4: Documentation")
        documenter = self.get_agent("documenter")
        if documenter:
            self.invoke_agent("documenter", context)
        
        self._logger.info("=== Workflow Complete ===")
        
        return AgentResult.ok({
            "message": "Custom workflow completed",
            "phases_completed": ["planning", "implementation", "testing", "documentation"],
        })


def main():
    result = AgentChain() \
        .requirements("Build a blog platform") \
        .orchestrator(MyOrchestrator) \
        .model("deepseek-coder-v2:16b") \
        .output("./blog") \
        .run()
    
    print(f"Custom workflow result: {result.success}")


if __name__ == "__main__":
    main()
