"""Desktop automation routes backed by the local MCP desktop server."""

from __future__ import annotations

from fastapi import APIRouter

from app.models.desktop_automation_schemas import (
    DesktopActionResponse,
    DesktopHealthResponse,
    ZaloDownloadLatestFileRequest,
    ZaloSendRequest,
    ZaloVerifyChatRequest,
)
from app.modules.integrations.application.desktop_automation_adapter import DesktopAutomationAdapter
from app.modules.zalo_crm.application.zalo_automation_service import ZaloAutomationService

router = APIRouter(prefix="/v1/desktop", tags=["desktop-automation"])


def _build_zalo_service() -> ZaloAutomationService:
    """Create the current local Zalo automation service."""
    return ZaloAutomationService(adapter=DesktopAutomationAdapter())


@router.get("/health", response_model=DesktopHealthResponse)
async def desktop_health() -> DesktopHealthResponse:
    """Return desktop automation health metadata."""

    return DesktopHealthResponse()


@router.post("/zalo/send", response_model=DesktopActionResponse)
async def zalo_send(request: ZaloSendRequest) -> DesktopActionResponse:
    """Execute the currently supported Zalo send workflow."""

    service = _build_zalo_service()

    if request.target_type == "my_document":
        result = await service.send_message_to_my_document(
            request.message,
            verify_before_send=request.verify_before_send,
            press_enter=request.press_enter,
            capture_evidence=request.capture_evidence,
            workflow_id=request.workflow_id,
            message_label=request.message_label,
        )
        return DesktopActionResponse.from_adapter_result(result)

    result = await service.send_message_to_contact(
        contact_name=request.target_name or "",
        message=request.message,
    )
    return DesktopActionResponse.from_adapter_result(result)


@router.post("/zalo/verify-chat", response_model=DesktopActionResponse)
async def zalo_verify_chat(request: ZaloVerifyChatRequest) -> DesktopActionResponse:
    """Verify the currently active Zalo chat through the adapter."""

    service = _build_zalo_service()
    result = await service.adapter.zalo_verify_active_chat(
        expected_name=request.target_name,
        allow_partial_match=request.allow_partial_match,
    )
    return DesktopActionResponse.from_adapter_result(result)


@router.post("/zalo/download-latest-file", response_model=DesktopActionResponse)
async def zalo_download_latest_file(
    request: ZaloDownloadLatestFileRequest,
) -> DesktopActionResponse:
    """Download the latest file from a verified Zalo chat into a project folder."""

    service = _build_zalo_service()
    result = await service.download_latest_file_to_project(
        target_name=request.target_name,
        project_path=request.project_path,
        subfolder=request.subfolder,
        download_dir=request.download_dir,
    )
    return DesktopActionResponse.from_adapter_result(result)
