/**
 * 简历存储和分析功能示例
 * 演示如何使用 get_online_resume 功能获取、存储和分析简历
 */

const jobManager = require('../api/middleware/job/jobManager');
const db = require('../api/middleware/dbProxy');

/**
 * 示例1: 获取在线简历并自动存储
 */
async function example1_getAndStoreResume() {
    console.log('\n========== 示例1: 获取在线简历并自动存储 ==========\n');

    try {
        // 模拟MQTT客户端
        const mockMqttClient = {
            publishAndWait: async (sn_code, command) => {
                console.log(`发送MQTT指令: ${command.action} 到设备 ${sn_code}`);
                
                // 模拟返回简历数据（实际应该从MQTT获取）
                return {
                    code: 200,
                    message: '成功',
                    data: require('../_doc/在线简历响应文本.json')
                };
            }
        };

        // 调用获取简历方法
        const resumeData = await jobManager.get_online_resume(
            'GHJU',
            mockMqttClient,
            { platform: 'boss' }
        );

        console.log('\n✅ 简历获取成功！');
        console.log('姓名:', resumeData.baseInfo?.name);
        console.log('工作年限:', resumeData.baseInfo?.workYearDesc);
        console.log('期望职位:', resumeData.expectList?.[0]?.positionName);
        console.log('期望薪资:', resumeData.expectList?.[0]?.salaryDesc);

    } catch (error) {
        console.error('❌ 获取简历失败:', error.message);
    }
}

/**
 * 示例2: 查询已存储的简历
 */
async function example2_queryStoredResume() {
    console.log('\n========== 示例2: 查询已存储的简历 ==========\n');

    try {
        const resume_info = db.getModel('resume_info');

        // 查询指定设备的简历
        const resume = await resume_info.findOne({
            where: {
                sn_code: 'GHJU',
                platform: 'boss',
                isActive: true
            }
        });

        if (resume) {
            console.log('✅ 找到简历记录：');
            console.log('简历ID:', resume.id);
            console.log('姓名:', resume.fullName);
            console.log('工作年限:', resume.workYears);
            console.log('期望职位:', resume.expectedPosition);
            console.log('期望薪资:', resume.expectedSalary);
            console.log('\nAI分析结果：');
            console.log('技能标签:', resume.aiSkillTags);
            console.log('优势:', resume.aiStrengths);
            console.log('劣势:', resume.aiWeaknesses);
            console.log('职业建议:', resume.aiCareerSuggestion);
            console.log('竞争力评分:', resume.aiCompetitiveness);
        } else {
            console.log('❌ 未找到简历记录');
        }

    } catch (error) {
        console.error('❌ 查询简历失败:', error.message);
    }
}

/**
 * 示例3: 查看简历的项目经验
 */
async function example3_viewProjectExperience() {
    console.log('\n========== 示例3: 查看简历的项目经验 ==========\n');

    try {
        const resume_info = db.getModel('resume_info');

        const resume = await resume_info.findOne({
            where: {
                sn_code: 'GHJU',
                platform: 'boss',
                isActive: true
            }
        });

        if (resume && resume.projectExperience) {
            const projects = JSON.parse(resume.projectExperience);
            
            console.log(`✅ 共有 ${projects.length} 个项目经验：\n`);
            
            projects.slice(0, 3).forEach((project, index) => {
                console.log(`项目 ${index + 1}: ${project.name}`);
                console.log(`角色: ${project.role}`);
                console.log(`时间: ${project.startDate} - ${project.endDate || '至今'}`);
                console.log(`描述: ${project.description?.substring(0, 100)}...`);
                console.log('---');
            });
        } else {
            console.log('❌ 未找到项目经验');
        }

    } catch (error) {
        console.error('❌ 查看项目经验失败:', error.message);
    }
}

/**
 * 示例4: 统计简历数据
 */
async function example4_resumeStatistics() {
    console.log('\n========== 示例4: 统计简历数据 ==========\n');

    try {
        const resume_info = db.getModel('resume_info');

        // 统计总数
        const totalCount = await resume_info.count();
        
        // 统计各平台数量
        const bossPlatformCount = await resume_info.count({ where: { platform: 'boss' } });
        
        // 统计高竞争力简历
        const highCompetitiveness = await resume_info.count({
            where: {
                aiCompetitiveness: { [db.models.op.gte]: 80 }
            }
        });

        console.log('✅ 简历统计：');
        console.log('总简历数:', totalCount);
        console.log('Boss直聘平台:', bossPlatformCount);
        console.log('高竞争力简历(≥80分):', highCompetitiveness);

    } catch (error) {
        console.error('❌ 统计失败:', error.message);
    }
}

// 主函数
async function main() {
    console.log('🚀 简历存储和分析功能示例\n');

    // 运行示例
    await example1_getAndStoreResume();
    await example2_queryStoredResume();
    await example3_viewProjectExperience();
    await example4_resumeStatistics();

    console.log('\n✨ 所有示例执行完成！\n');
}

// 如果直接运行此文件
if (require.main === module) {
    main().catch(error => {
        console.error('执行失败:', error);
        process.exit(1);
    });
}

module.exports = {
    example1_getAndStoreResume,
    example2_queryStoredResume,
    example3_viewProjectExperience,
    example4_resumeStatistics
};

