"""
Automated Test Suite - Tag Search Agent
12 queries, validate: products, reasoning, images, prices.
"""
import sys, asyncio, time
sys.stdout.reconfigure(encoding='utf-8', errors='replace')

import aiohttp

API_URL = "http://localhost:5000/api/tag-search/chat"

TEST_CASES = [
    ("áo phông nam", 1, "Loai co ban - ao phong nam"),
    ("quần jean nữ", 1, "Loai co ban - quan jean nu"),
    ("sịp nam", 1, "Tu long - sip (quan lot)"),
    ("áo mùa đông cho bé gái", 1, "Thoi tiet + gender - mua dong be gai"),
    ("váy đi tiệc cho nữ", 1, "Occasion - vay di tiec"),
    ("đồ thể thao nữ", 1, "Occasion - the thao"),
    ("áo 2/9", 1, "Ngay le -> suy luan mau do"),
    ("áo khoác gió nam", 1, "Loai cu the - ao khoac gio"),
    ("hàng mới về", 1, "Discovery mode - hang moi"),
    ("áo polo nam đi làm", 1, "Loai + Occasion - polo di lam"),
    ("quần đi biển cho bé trai", 1, "Occasion + gender tre em"),
    ("đồ mặc nhà thoải mái cho nữ", 1, "Occasion - mac nha"),
]

async def test_query(session, query, expected_min, desc, test_num):
    sid = f"autotest_{int(time.time())}_{test_num}"
    try:
        async with session.post(API_URL, json={"query": query, "session_id": sid}, timeout=aiohttp.ClientTimeout(total=60)) as resp:
            data = await resp.json()
    except Exception as e:
        return {"test": test_num, "query": query, "desc": desc, "pass": False, "checks": [f"ERROR: {e}"], "products": 0, "reasoning": "", "elapsed_ms": 0, "sample": ""}

    products = data.get("products", [])
    pipeline = data.get("pipeline", [])
    api_elapsed = data.get("elapsed_ms", 0)

    reasoning = ""
    for step in pipeline:
        if step.get("step") == "planner" and step.get("tool_calls"):
            for tc in step["tool_calls"]:
                r = tc.get("args", {}).get("reasoning", "")
                if r: reasoning = r

    checks = []
    ok = True

    if data.get("status") == "success":
        checks.append("[OK] status")
    else:
        checks.append("[FAIL] status=" + data.get("status", "?"))
        ok = False

    pc = len(products)
    if pc >= expected_min:
        checks.append(f"[OK] {pc} SP")
    else:
        checks.append(f"[FAIL] {pc} SP (need>={expected_min})")
        ok = False

    checks.append("[OK] reasoning" if reasoning else "[WARN] no reasoning")

    imgs = sum(1 for p in products if p.get("image", "").startswith("http"))
    if products:
        checks.append(f"[OK] img {imgs}/{pc}" if imgs == pc else f"[WARN] img {imgs}/{pc}")

    prices = sum(1 for p in products if p.get("price", 0) > 0)
    if products:
        checks.append(f"[OK] price" if prices == pc else f"[WARN] price {prices}/{pc}")

    return {
        "test": test_num, "query": query, "desc": desc, "pass": ok,
        "checks": checks, "products": pc,
        "reasoning": reasoning[:80] if reasoning else "(none)",
        "elapsed_ms": api_elapsed,
        "sample": products[0].get("name", "") if products else "(none)",
    }

async def main():
    print("=" * 70)
    print("  TAG SEARCH AGENT - AUTOMATED TEST SUITE")
    print(f"  {len(TEST_CASES)} test cases -> {API_URL}")
    print("=" * 70)

    results = []
    async with aiohttp.ClientSession() as session:
        for i, (query, exp, desc) in enumerate(TEST_CASES, 1):
            print(f"[{i:2d}/{len(TEST_CASES)}] Testing: \"{query}\" ...", end="", flush=True)
            r = await test_query(session, query, exp, desc, i)
            results.append(r)
            tag = "PASS" if r["pass"] else "FAIL"
            print(f" {tag} | {r['products']} SP | {r['elapsed_ms']}ms")
            await asyncio.sleep(0.3)

    print("\n" + "=" * 70)
    passed = sum(1 for r in results if r["pass"])
    failed = len(results) - passed
    print(f"  RESULTS: {passed}/{len(results)} passed, {failed} failed\n")

    for r in results:
        icon = "PASS" if r["pass"] else "FAIL"
        print(f"[{icon}] #{r['test']:2d} | {r['desc']}")
        print(f"   Query:   \"{r['query']}\"")
        print(f"   Checks:  {' | '.join(r['checks'])}")
        print(f"   Product: {r['sample']} | {r['elapsed_ms']}ms")
        print(f"   Reason:  {r['reasoning']}")
        print()

    if failed:
        print("!! FAILED TESTS:")
        for r in results:
            if not r["pass"]:
                fails = [c for c in r["checks"] if "[FAIL]" in c]
                print(f"  #{r['test']} \"{r['query']}\" -> {fails}")

    print(f"\nDone! {passed}/{len(results)} passed")

if __name__ == "__main__":
    asyncio.run(main())
