from __future__ import annotations

import logging
from typing import Any


logger = logging.getLogger(__name__)


class TelegramNutritionOnboardingRuntimeNoticeMixin:
    @staticmethod
    async def _retire_keyboard(query: Any) -> None:
        edit = getattr(query, "edit_message_reply_markup", None)
        if not callable(edit):
            return
        try:
            await edit(reply_markup=None)
        except (OSError, RuntimeError, TypeError, ValueError):
            logger.info(
                "Nutrition onboarding keyboard retirement failed",
                exc_info=True,
            )

    async def _send_customer_lifecycle_notice(
        self,
        session: Any,
        lifecycle: str,
    ) -> None:
        notices = {
            "revision": (
                "검토 중 수정이 요청되어 영양 온보딩을 다시 열었습니다. "
                "새 온보딩 카드에서 입력을 확인해 주세요."
            ),
            "safety": (
                "안전 확인을 위해 영양 온보딩을 보류했습니다. "
                "운영자가 다음 안내를 드립니다."
            ),
            "rejection": (
                "운영 검토에서 영양 온보딩이 승인되지 않았습니다. "
                "담당자에게 문의해 주세요."
            ),
        }
        notice = (
            notices[lifecycle]
            + "\n아직 영양 코칭은 시작되거나 활성화되지 않았습니다."
        )
        route = self._route(session, "customer")
        try:
            await self.adapter._send_nutrition_topic(
                chat_id=route[0],
                topic_id=route[1],
                text=notice,
            )
        except (OSError, RuntimeError, TypeError, ValueError):
            logger.info(
                "Nutrition onboarding customer lifecycle notice failed",
                exc_info=True,
            )

    async def _send_customer_confirmation(
        self,
        session: Any,
        status: Any,
    ) -> None:
        route = self._route(session, "customer")
        notice = (
            "입력 확인이 저장됐어요.\n"
            + (
                "안전 확인이 필요해 온보딩을 잠시 멈췄습니다. "
                "운영자가 다음 안내를 드립니다.\n"
                if status.state.value == "safety_hold"
                else "이제 운영자가 내용을 검토합니다.\n"
            )
            + "아직 영양 코칭은 시작되거나 활성화되지 않았습니다."
        )
        try:
            await self.adapter._send_nutrition_topic(
                chat_id=route[0],
                topic_id=route[1],
                text=notice,
            )
        except (OSError, RuntimeError, TypeError, ValueError):
            logger.info(
                "Nutrition onboarding customer confirmation notice failed",
                exc_info=True,
            )
