from __future__ import annotations

import logging
from typing import Any

logger = logging.getLogger(__name__)


class DrawingPipelineChatMixin:
    async def chat_train_ai_quynh(
        self,
        takeoff_id: str,
        user_message: str,
        current_user_name: str = "Kỹ Sư Trưởng QS",
        auto_apply: bool = False,
    ) -> dict[str, Any]:
        """Tương tác huấn luyện trực tiếp với AI Quỳnh QS, giải trình kỹ thuật và rút kinh nghiệm vĩnh viễn."""
        from app.modules.takeoff.application.learning_memory import TakeoffLearningMemoryEngine

        engine = TakeoffLearningMemoryEngine(self._erp_client, self._llm_client)
        return await engine.chat_train_ai_quynh(
            takeoff_id=takeoff_id,
            user_message=user_message,
            current_user_name=current_user_name,
            auto_apply=auto_apply,
        )

    async def chat_train_ai_quynh_stream(
        self,
        takeoff_id: str,
        user_message: str,
        current_user_name: str = "Kỹ Sư Trưởng QS",
        auto_apply: bool = False,
    ):
        """Streaming generator gửi phản hồi thời gian thực qua Server-Sent Events (SSE)."""
        from app.modules.takeoff.application.learning_memory import TakeoffLearningMemoryEngine

        engine = TakeoffLearningMemoryEngine(self._erp_client, self._llm_client)
        async for chunk in engine.chat_train_ai_quynh_stream(
            takeoff_id=takeoff_id,
            user_message=user_message,
            current_user_name=current_user_name,
            auto_apply=auto_apply,
        ):
            yield chunk
