import asyncio
import os
import sys
import time

# Add parent directory to path to import common and agent
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from agent.tools.data_retrieval_tool import SearchItem, data_retrieval_tool


async def verify_optimization():
    print("🚀 Verifying Performance Optimization...")
    
    search_1 = {
        "description": "váy tiểu thư sang chảnh",
        "product_name": "Váy",
        "gender_by_product": "Nữ",
        "age_by_product": "Người lớn",
        "magento_ref_code": None,
        "price_min": None,
        "price_max": None,
        "master_color": None,
        "form_sleeve": None,
        "style": None,
        "fitting": None,
        "form_neckline": None,
        "material_group": None,
        "season": None,
        "product_line_vn": None
    }
    
    search_2 = {
        "description": "áo thun nam basic",
        "product_name": "Áo thun",
        "gender_by_product": "Nam",
        "age_by_product": "Người lớn",
        "master_color": "Đen",
        "magento_ref_code": None,
        "price_min": None,
        "price_max": None,
        "form_sleeve": None,
        "style": None,
        "fitting": None,
        "form_neckline": None,
        "material_group": None,
        "season": None,
        "product_line_vn": None
    }
    
    test_searches = [
        SearchItem.model_validate(search_1),
        SearchItem.model_validate(search_2)
    ]
    
    start_total = time.time()
    result_str = await data_retrieval_tool.ainvoke({"searches": test_searches})
    end_total = time.time()
    
    print(f"\nRESULTS FOR SEARCHES:")
    print(f"Total Tool Execution Time: {(end_total - start_total) * 1000:.2f}ms")
    
    # Check query.txt
    query_txt_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "agent", "tools", "query.txt")
    if os.path.exists(query_txt_path):
        with open(query_txt_path, "r", encoding="utf-8") as f:
            query_content = f.read()
            print("\nLAST SQL QUERY (First 5 lines):")
            print("\n".join(query_content.splitlines()[:5]))
    else:
        print("\n⚠️ query.txt not found!")

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