"""
Unified Orchestrator - Combines Task Workflows
Implements Agent-as-Tools pattern with shared components and strict workflow enforcement
"""

from strands import Agent, tool
from shared_components import workflow_state
from task_create_agent import (
    task_create_agent,
)

# ============================================================================
# UNIFIED AGENT-AS-TOOLS
# ============================================================================

@tool
async def handle_task_creation(chat_id: str, request: str) -> str:
    """Handle task creation requests with strict workflow"""
    try:
        state = workflow_state.get_state(chat_id)
        if not state.get("type"):
            workflow_state.set_state(chat_id, {"type": "task_creation", "step": 1})
        
        context = f"Chat ID: {chat_id}\nRequest: {request}"
        response = task_create_agent(context)
        return str(response)
    except Exception as e:
        return f"Error processing task creation: {str(e)}"

# ============================================================================
# UNIFIED ORCHESTRATOR AGENT
# ============================================================================

unified_orchestrator = Agent(
    name="UnifiedOrchestrator",
    system_prompt="""Route requests to specialized workflow agents. Be concise and natural.

**ROUTING LOGIC:**
🔧 **Task Workflows:**
- Task Creation → handle_task_creation

**RESPONSE STYLE:**
- Remove agent or tool names from responses
- MAKE SURE to forward the full user message to the appropriate agent
- DO NOT alter the response from the specialized agents
- MAKE SURE to forward the full agent message back to the user
- Follow specialized agent workflows exactly
- Maintain conversation context across turns

**WORKFLOW ENFORCEMENT:**
- Each workflow has strict step sequences that cannot be skipped
- Agents track their current step and validate progression
- Confirmation required before any submission actions
- State management preserves context across conversation turns""",
    model="amazon.nova-pro-v1:0",
    callback_handler=None,
    tools=[
        handle_task_creation,
    ]
)

# Export the unified orchestrator
__all__ = ["unified_orchestrator"]