from .base import BaseAgent
from ..db import create_task, update_task_status, add_log
from .. import service_proxy
from ..telegram import messaging
from ..telegram.realestate_message import format_realestate_matches, build_match_keyboard


class RealestateAgent(BaseAgent):
    """부동산 청약 에이전트.

    realestate-lab이 신규 매칭 발견 시 /realestate/notify로 push해 트리거됨.
    on_new_matches가 메인 진입점. on_schedule은 사용하지 않음(cron 폐기).
    """

    agent_id = "realestate"
    display_name = "청약 애널리스트"

    async def on_new_matches(self, matches: list[dict]) -> dict:
        """신규 매칭 N건을 텔레그램 1통으로 푸시.
        성공 시 sent_ids 반환 → realestate-lab이 notified_at 마킹.
        실패 시 sent=0, sent_ids=[] 반환 → 다음 사이클 재시도.
        """
        if not matches:
            return {"sent": 0, "sent_ids": []}

        task_id = create_task(self.agent_id, "notify_matches", {"count": len(matches)})

        try:
            text = format_realestate_matches(matches)
            keyboard = build_match_keyboard(matches)
            await self.transition("reporting", f"매칭 {len(matches)}건 알림", task_id)

            tg = await messaging.send_raw(text, reply_markup=keyboard)
            if not tg.get("ok"):
                update_task_status(task_id, "failed", {"error": tg.get("description")})
                await self.transition("idle", "알림 실패")
                return {"sent": 0, "sent_ids": [], "error": tg.get("description")}

            sent_ids = [m["id"] for m in matches if "id" in m]
            update_task_status(task_id, "succeeded", {
                "sent": len(matches),
                "telegram_message_id": tg.get("message_id"),
            })
            await self.transition("idle", f"매칭 {len(matches)}건 알림 완료")
            return {
                "sent": len(matches),
                "sent_ids": sent_ids,
                "message_id": tg.get("message_id"),
            }
        except Exception as e:
            add_log(self.agent_id, f"on_new_matches failed: {e}", "error", task_id)
            update_task_status(task_id, "failed", {"error": str(e)})
            await self.transition("idle", f"오류: {e}")
            return {"sent": 0, "sent_ids": [], "error": str(e)}

    async def on_command(self, command: str, params: dict) -> dict:
        if command == "fetch_matches":
            try:
                matches = await service_proxy.realestate_matches(limit=20)
                if not matches:
                    return {"ok": True, "message": "매칭 없음"}
                result = await self.on_new_matches(matches)
                return {"ok": True, "result": result}
            except Exception as e:
                return {"ok": False, "message": str(e)}

        if command == "dashboard":
            try:
                data = await service_proxy.realestate_dashboard()
                return {"ok": True, "dashboard": data}
            except Exception as e:
                return {"ok": False, "message": str(e)}

        return {"ok": False, "message": f"Unknown command: {command}"}

    async def on_approval(self, task_id: str, approved: bool, feedback: str = "") -> None:
        pass
