#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
测试脚本：验证 Phase 1 关键词提取优化

测试项：
1. jieba分词替代暴力替换
2. 修饰词精简后不再空关键词
3. 中文值提取
"""

import sys
import os
import io

# 设置标准输出编码为UTF-8（解决Windows GBK编码问题）
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.services.keyword_matcher import extract_keywords, extract_value_from_description


def test_extract_keywords():
    """测试关键词提取"""
    print("=" * 60)
    print("测试关键词提取（jieba分词版本）")
    print("=" * 60)

    test_cases = [
        # (输入, 期望包含的关键词)
        ("点击【新建会议】按钮", ["新建会议"]),
        ("点击展开", ["展开"]),  # 之前返回空列表
        ("点击系统设置", ["系统设置"]),  # 之前返回空列表
        ("输入会议名称：自动化新建会议", ["会议名称", "自动化", "新建会议"]),
        ("点击信息发布", ["信息发布"]),
        ("点击功能中心图标", ["功能中心"]),
        ("输入备注：这是一个测试备注", ["备注"]),
        ("点击确定按钮", ["确定"]),
        ("点击取消按钮", ["取消"]),
        ("输入用户名：admin@xty", ["用户名"]),
    ]

    passed = 0
    failed = 0

    for desc, expected_keywords in test_cases:
        result = extract_keywords(desc)

        # 检查期望的关键词是否在结果中
        missing = [kw for kw in expected_keywords if kw not in result]

        if missing:
            print(f"[FAIL] '{desc}'")
            print(f"   期望包含: {expected_keywords}")
            print(f"   实际结果: {result}")
            print(f"   缺失: {missing}")
            failed += 1
        else:
            print(f"[PASS] '{desc}' -> {result}")
            passed += 1

    print()
    print(f"结果: 通过 {passed}/{passed + failed}")
    return failed == 0


def test_extract_value():
    """测试值提取"""
    print()
    print("=" * 60)
    print("测试值提取（支持中文值）")
    print("=" * 60)

    test_cases = [
        # (输入, 期望值)
        ("输入用户名：admin@xty", "admin@xty"),
        ("输入账号：admin@xty", "admin@xty"),
        ("输入密码：Ubains@13579", "Ubains@13579"),
        ("输入参考价值：100", "100"),
        ("输入备注：这是一个测试备注", "这是一个测试备注"),
        ("输入会议名称：自动化测试会议", "自动化测试会议"),
        ("输入用户名 admin@xty", "admin@xty"),
        ("输入值'test_value'", "test_value"),
        ("输入值\"test_value\"", "test_value"),
    ]

    passed = 0
    failed = 0

    for desc, expected_value in test_cases:
        result = extract_value_from_description(desc)

        if result == expected_value:
            print(f"[PASS] '{desc}' -> '{result}'")
            passed += 1
        else:
            print(f"[FAIL] '{desc}'")
            print(f"   期望: '{expected_value}'")
            print(f"   实际: '{result}'")
            failed += 1

    print()
    print(f"结果: 通过 {passed}/{passed + failed}")
    return failed == 0


def main():
    """运行所有测试"""
    print("\n" + "=" * 60)
    print("Phase 1 关键词提取优化 - 验证测试")
    print("=" * 60)

    all_passed = True

    # 测试关键词提取
    if not test_extract_keywords():
        all_passed = False

    # 测试值提取
    if not test_extract_value():
        all_passed = False

    print()
    print("=" * 60)
    if all_passed:
        print("[SUCCESS] 所有测试通过！Phase 1 验证成功")
    else:
        print("[WARNING] 部分测试失败，请检查")
    print("=" * 60)

    return 0 if all_passed else 1


if __name__ == "__main__":
    sys.exit(main())