"""
Test Color Fallback Mechanism
Demonstrates how color filter works with intelligent fallback.
"""

from agent.tools.data_retrieval_filter import filter_color_with_smart_fallback

# Mock products with different colors
MOCK_PRODUCTS = [
    {"master_color": "Đen/ Black", "product_color_name": "Black 001", "name": "Áo polo đen"},
    {"master_color": "Trắng/ White", "product_color_name": "White 100", "name": "Váy trắng"},
    {"master_color": "Trắng/ White", "product_color_name": "White 101", "name": "Áo trắng"},
    {"master_color": "Be/ Beige", "product_color_name": "Beige 050", "name": "Quần be"},
    {"master_color": "Xanh da trời/ Blue", "product_color_name": "Blue 200", "name": "Áo xanh"},
    {"master_color": "Màu xanh Jeans", "product_color_name": "Jeans 210", "name": "Quần jean"},
    {"master_color": "Nâu/ Brown", "product_color_name": "Brown 300", "name": "Áo nâu"},
    {"master_color": "Đỏ/ Red", "product_color_name": "Red 400", "name": "Váy đỏ"},
]


def test_case(requested_color: str):
    """Test một case và in kết quả"""
    print(f"\n{'=' * 80}")
    print(f"TEST: User hỏi màu '{requested_color}'")
    print("=" * 80)

    filtered, info = filter_color_with_smart_fallback(MOCK_PRODUCTS, requested_color)

    print(f"✅ Kết quả: {len(filtered)} sản phẩm")
    print(f"📊 Fallback used: {info.get('fallback_used')}")
    print(f"🎨 Requested: {info.get('requested_value')}")
    print(f"🎯 Matched: {info.get('matched_value')}")

    if info.get("message"):
        print("\n💬 RECOMMENDATION MESSAGE (trả về cho user):")
        print(f"   {info['message']}")

    if info.get("alternative_colors"):
        print("\n💡 Alternative colors:")
        for alt in info["alternative_colors"]:
            print(f"   - {alt}")

    print("\n📦 Products returned:")
    for p in filtered[:3]:  # Show top 3
        print(f"   - {p['name']} ({p['master_color']})")

    if len(filtered) > 3:
        print(f"   ... và {len(filtered) - 3} sản phẩm khác")


if __name__ == "__main__":
    print("\n" + "=" * 80)
    print("COLOR FALLBACK TEST SUITE")
    print("=" * 80)

    # Case 1: Exact match
    test_case("Trắng")

    # Case 2: Compound color - fallback
    test_case("Trắng kem")

    # Case 3: Navy → Jeans fallback
    test_case("Xanh navy")

    # Case 4: Chocolate → Brown fallback
    test_case("Nâu chocolate")

    # Case 5: No match at all
    test_case("Vàng chanh")

    print("\n" + "=" * 80)
    print("DONE! ✅")
    print("=" * 80 + "\n")
