"""Clean execution service implementation without mocks."""

import logging
from typing import Any
from uuid import uuid4

from ..domain.interfaces import (
    ExecutionRequest,
    ExecutionResult,
    ExecutionServiceInterface,
)

logger = logging.getLogger(__name__)


class ExecutionService(ExecutionServiceInterface):
    """Clean execution service that delegates to workflow orchestrators."""

    def __init__(self, workflow_orchestrator: "WorkflowOrchestratorInterface"):
        self._workflow_orchestrator = workflow_orchestrator

    @property
    def orchestrator(self) -> "WorkflowOrchestratorInterface":
        return self._workflow_orchestrator

    async def execute_async(self, request: ExecutionRequest) -> ExecutionResult:
        """Execute agent task via workflow orchestrator."""
        try:
            task_id = str(uuid4())
            execution_id = f"task-{task_id}"

            # Delegate to workflow orchestrator
            result = await self._workflow_orchestrator.start_workflow(
                execution_id=execution_id, request=request
            )

            return ExecutionResult(
                task_id=task_id,
                execution_id=execution_id,
                success=result.get("success", True),
                status=result.get("status", "started"),
                content=result.get("content", "Task started"),
                metadata=result.get("metadata", {}),
            )

        except Exception as e:
            logger.error(f"Failed to start execution: {e}")
            task_id = str(uuid4())
            execution_id = f"task-{task_id}"

            return ExecutionResult(
                task_id=task_id,
                execution_id=execution_id,
                success=False,
                status="failed",
                error=str(e),
            )

    async def get_status(self, execution_id: str) -> dict[str, Any]:
        """Get execution status from workflow orchestrator."""
        try:
            return await self._workflow_orchestrator.get_workflow_status(execution_id)
        except Exception as e:
            logger.error(f"Failed to get execution status: {e}")
            return {
                "status": "error",
                "success": False,
                "error": str(e),
            }

    async def cancel_execution(self, execution_id: str) -> bool:
        """Cancel execution via workflow orchestrator."""
        try:
            return await self._workflow_orchestrator.cancel_workflow(execution_id)
        except Exception as e:
            logger.error(f"Failed to cancel execution: {e}")
            return False

    async def pause_execution(self, execution_id: str) -> bool:
        """Pause execution via workflow orchestrator."""
        try:
            return await self._workflow_orchestrator.pause_workflow(execution_id)
        except Exception as e:
            logger.error(f"Failed to pause execution: {e}")
            return False

    async def resume_execution(self, execution_id: str) -> bool:
        """Resume execution via workflow orchestrator."""
        try:
            return await self._workflow_orchestrator.resume_workflow(execution_id)
        except Exception as e:
            logger.error(f"Failed to resume execution: {e}")
            return False

    async def send_a2ui_action(self, execution_id: str, action_data: dict) -> bool:
        """Send A2UI user action to a running workflow."""
        try:
            return await self._workflow_orchestrator.send_a2ui_action(execution_id, action_data)
        except Exception as e:
            logger.error(f"Failed to send A2UI action: {e}")
            return False

    async def resolve_escalation(
        self,
        execution_id: str,
        escalation_id: str,
        approved: bool,
        comment: str = "",
        resolved_by: str = "",
    ) -> bool:
        """Resolve a tool escalation via workflow orchestrator."""
        try:
            return await self._workflow_orchestrator.resolve_escalation_workflow(
                execution_id, escalation_id, approved, comment, resolved_by
            )
        except Exception as e:
            logger.error(f"Failed to resolve escalation: {e}")
            return False

    async def send_workflow_command(
        self, execution_id: str, command: str, payload: dict[str, Any]
    ) -> bool:
        """Send a generic command signal to a running workflow."""
        try:
            return await self._workflow_orchestrator.send_workflow_command(
                execution_id, command, payload
            )
        except Exception as e:
            logger.error(f"Failed to send workflow command '{command}': {e}")
            return False


# Interface for workflow orchestrators
from abc import ABC, abstractmethod  # noqa: E402


class WorkflowOrchestratorInterface(ABC):
    """Interface for workflow orchestration systems."""

    @abstractmethod
    async def start_workflow(self, execution_id: str, request: ExecutionRequest) -> dict[str, Any]:
        """Start workflow execution."""
        pass

    @abstractmethod
    async def get_workflow_status(self, execution_id: str) -> dict[str, Any]:
        """Get workflow status."""
        pass

    @abstractmethod
    async def cancel_workflow(self, execution_id: str) -> bool:
        """Cancel workflow."""
        pass

    @abstractmethod
    async def pause_workflow(self, execution_id: str) -> bool:
        """Pause workflow execution."""
        pass

    @abstractmethod
    async def resume_workflow(self, execution_id: str) -> bool:
        """Resume paused workflow execution."""
        pass

    @abstractmethod
    async def send_a2ui_action(self, execution_id: str, action_data: dict) -> bool:
        """Send A2UI action to a running workflow."""
        pass

    @abstractmethod
    async def resolve_escalation_workflow(
        self,
        execution_id: str,
        escalation_id: str,
        approved: bool,
        comment: str = "",
        resolved_by: str = "",
    ) -> bool:
        """Resolve a tool escalation in the workflow."""
        pass

    @abstractmethod
    async def send_workflow_command(
        self, execution_id: str, command: str, payload: dict[str, Any]
    ) -> bool:
        """Send a generic command signal to a running workflow."""
        pass
