import unittest
from unittest.mock import AsyncMock, patch

from core.pydantic_ai_agent import CustomerMessage, CustomerServiceAgent


class BatchQuoteReplyFormatTest(unittest.IsolatedAsyncioTestCase):
    async def test_batch_reply_contains_per_image_and_options(self):
        agent = CustomerServiceAgent()
        cid = "__batch_quote_case__"
        st = agent._get_conversation_state(cid)
        st.pending_image_urls = ["https://img.alicdn.com/a.jpg", "https://img.alicdn.com/b.jpg"]
        st.pending_requirements = ["去背景", "加急"]

        msg = CustomerMessage(
            msg_id="m-batch-1",
            acc_id="test_shop",
            msg="发完了，统一报价",
            from_id=cid,
            from_name="t",
            cy_id=cid,
            acc_type="AliWorkbench",
            msg_type=0,
            cy_name="t",
            goods_name="专业找图",
            goods_order="",
        )

        fake_r1 = {
            "complexity": "normal",
            "reason": "常规处理",
            "price_min": 15,
            "price_max": 25,
            "price_suggest": 20,
            "feasibility": "yes",
            "risk": "low",
            "aspect_ratio": "1:1",
            "perspective": "no",
        }
        fake_r2 = {
            "complexity": "complex",
            "reason": "细节较多",
            "price_min": 20,
            "price_max": 30,
            "price_suggest": 25,
            "feasibility": "yes",
            "risk": "low",
            "aspect_ratio": "1:1",
            "perspective": "no",
        }

        with patch("image.image_analyzer.image_analyzer.analyze", new=AsyncMock(side_effect=[fake_r1, fake_r2])):
            with patch("core.workflow.workflow.image_analysis_result", new=AsyncMock(return_value=None)):
                res = await agent._quote_pending_images(st, msg)

        self.assertFalse(res.get("need_transfer", False))
        reply = res.get("reply", "")
        self.assertIn("图1", reply)
        self.assertIn("图2", reply)
        self.assertIn("可选", reply)
        self.assertIn("打包", reply)
        self.assertIn("共", reply)

    async def test_single_image_reply_avoids_batch_wording(self):
        agent = CustomerServiceAgent()
        results = [
            (
                "https://img.alicdn.com/a.jpg",
                {
                    "complexity": "normal",
                    "reason": "常规处理",
                    "price_suggest": 20,
                },
            )
        ]
        reply = agent._build_batch_quote_reply(
            results=results,
            total_suggest=20,
            bundle_price=20,
            req_fee={"extra": 0, "hits": []},
        )
        self.assertIn("这张", reply)
        self.assertNotIn("这批", reply)
        self.assertNotIn("先给你分图报下", reply)
        self.assertNotIn("可选：A", reply)


if __name__ == "__main__":
    unittest.main(verbosity=2)
