#!/usr/bin/env python3
"""
测试修改后的parse_resume_with_qwen函数
"""

import os
import sys
import logging
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)

def test_parse_resume_with_qwen():
    """测试parse_resume_with_qwen函数"""
    try:
        # 导入函数
        from app.core.data_parse.parse_resume import parse_resume_with_qwen
        
        # 测试文件路径
        test_file = "方全.pdf"
        
        # 检查文件是否存在
        if not os.path.exists(test_file):
            print(f"错误: 测试文件 {test_file} 不存在")
            return False
        
        print(f"开始测试 parse_resume_with_qwen 函数...")
        print(f"测试文件: {test_file}")
        print(f"文件大小: {os.path.getsize(test_file)} 字节")
        
        # 调用函数
        result = parse_resume_with_qwen(test_file)
        
        # 输出结果
        print("\n=== 解析结果 ===")
        print(f"姓名(中文): {result.get('name_zh', 'N/A')}")
        print(f"姓名(英文): {result.get('name_en', 'N/A')}")
        print(f"职位(中文): {result.get('title_zh', 'N/A')}")
        print(f"职位(英文): {result.get('title_en', 'N/A')}")
        print(f"公司(中文): {result.get('hotel_zh', 'N/A')}")
        print(f"公司(英文): {result.get('hotel_en', 'N/A')}")
        print(f"手机号码: {result.get('mobile', 'N/A')}")
        print(f"固定电话: {result.get('phone', 'N/A')}")
        print(f"电子邮箱: {result.get('email', 'N/A')}")
        print(f"地址(中文): {result.get('address_zh', 'N/A')}")
        print(f"地址(英文): {result.get('address_en', 'N/A')}")
        print(f"生日: {result.get('birthday', 'N/A')}")
        print(f"年龄: {result.get('age', 'N/A')}")
        print(f"籍贯: {result.get('native_place', 'N/A')}")
        print(f"居住地: {result.get('residence', 'N/A')}")
        print(f"品牌组合: {result.get('brand_group', 'N/A')}")
        
        # 输出职业轨迹
        career_path = result.get('career_path', [])
        if career_path:
            print(f"\n职业轨迹 (共{len(career_path)}条):")
            for i, career in enumerate(career_path, 1):
                print(f"  {i}. 日期: {career.get('date', 'N/A')}")
                print(f"     公司: {career.get('hotel_zh', 'N/A')} / {career.get('hotel_en', 'N/A')}")
                print(f"     职位: {career.get('title_zh', 'N/A')} / {career.get('title_en', 'N/A')}")
        else:
            print("\n职业轨迹: 无")
        
        # 输出隶属关系
        affiliation = result.get('affiliation', [])
        if affiliation:
            print(f"\n隶属关系 (共{len(affiliation)}条):")
            for i, aff in enumerate(affiliation, 1):
                print(f"  {i}. 公司: {aff.get('company', 'N/A')}")
                print(f"     集团: {aff.get('group', 'N/A')}")
        else:
            print("\n隶属关系: 无")
        
        print("\n=== 测试完成 ===")
        return True
        
    except Exception as e:
        print(f"测试失败: {str(e)}")
        import traceback
        traceback.print_exc()
        return False

def test_parse_resume_file():
    """测试parse_resume_file函数"""
    try:
        # 导入函数
        from app.core.data_parse.parse_resume import parse_resume_file
        
        # 测试文件路径
        test_file = "方全.pdf"
        
        # 检查文件是否存在
        if not os.path.exists(test_file):
            print(f"错误: 测试文件 {test_file} 不存在")
            return False
        
        print(f"\n开始测试 parse_resume_file 函数...")
        print(f"测试文件: {test_file}")
        
        # 调用函数
        result = parse_resume_file(test_file)
        
        # 输出结果
        if result.get('success'):
            print("✓ 文件解析成功")
            data = result.get('data', {})
            print(f"解析时间: {data.get('parse_time', 'N/A')}")
            print(f"文件信息: {data.get('file_info', {})}")
            print(f"提取方法: {data.get('extraction_info', {}).get('extraction_method', 'N/A')}")
        else:
            print(f"✗ 文件解析失败: {result.get('error', '未知错误')}")
        
        return result.get('success', False)
        
    except Exception as e:
        print(f"测试失败: {str(e)}")
        import traceback
        traceback.print_exc()
        return False

if __name__ == "__main__":
    print("=" * 50)
    print("测试修改后的简历解析函数")
    print("=" * 50)
    
    # 测试parse_resume_with_qwen函数
    success1 = test_parse_resume_with_qwen()
    
    # 测试parse_resume_file函数
    success2 = test_parse_resume_file()
    
    print("\n" + "=" * 50)
    print("测试总结:")
    print(f"parse_resume_with_qwen: {'✓ 成功' if success1 else '✗ 失败'}")
    print(f"parse_resume_file: {'✓ 成功' if success2 else '✗ 失败'}")
    print("=" * 50) 