/**
 * 完整API测试脚本
 * 测试所有核心API接口功能
 */

const axios = require('axios');

const API_BASE = 'http://localhost:9097/api';
let testResults = {
  total: 0,
  passed: 0,
  failed: 0,
  skipped: 0
};

// 测试用的设备ID
const TEST_DEVICE_ID = 'test_device_' + Date.now();
let testResumeId = null;
let testJobId = null;
let testTaskId = null;

// 打印测试结果
function printResult(testName, passed, message = '') {
  testResults.total++;
  if (passed) {
    testResults.passed++;
    console.log(`✅ [PASS] ${testName}`);
    if (message) console.log(`   └─ ${message}`);
  } else {
    testResults.failed++;
    console.log(`❌ [FAIL] ${testName}`);
    if (message) console.log(`   └─ ${message}`);
  }
}

function printSkipped(testName, reason) {
  testResults.total++;
  testResults.skipped++;
  console.log(`⏭️  [SKIP] ${testName}`);
  console.log(`   └─ ${reason}`);
}

// API测试函数
async function testAPI(name, method, endpoint, data = null, expectedStatus = 200) {
  try {
    const url = `${API_BASE}${endpoint}`;
    let response;

    if (method === 'GET') {
      response = await axios.get(url, { params: data });
    } else if (method === 'POST') {
      response = await axios.post(url, data);
    } else if (method === 'PUT') {
      response = await axios.put(url, data);
    } else if (method === 'DELETE') {
      response = await axios.delete(url, { data });
    }

    const passed = response.status === expectedStatus;
    printResult(name, passed, `状态码: ${response.status}`);
    return { success: true, data: response.data };
  } catch (error) {
    const message = error.response
      ? `状态码: ${error.response.status}, 错误: ${error.response.data?.message || error.message}`
      : `网络错误: ${error.message}`;
    printResult(name, false, message);
    return { success: false, error: error.message };
  }
}

// 主测试流程
async function runTests() {
  console.log('='.repeat(60));
  console.log('🧪 开始API完整测试');
  console.log('='.repeat(60));
  console.log(`测试设备ID: ${TEST_DEVICE_ID}`);
  console.log('');

  // ============================================
  // 1. 健康检查
  // ============================================
  console.log('\n📋 【1/8】健康检查 API\n');
  await testAPI('健康检查', 'GET', '/health');

  // ============================================
  // 2. 设备管理
  // ============================================
  console.log('\n📋 【2/8】设备管理 API\n');

  const registerResult = await testAPI('设备注册', 'POST', '/device/register', {
    sn_code: TEST_DEVICE_ID,
    deviceName: '测试设备',
    clientVersion: '1.0.0',
    osVersion: 'Windows 10',
    pythonVersion: '3.9.0'
  });

  await testAPI('设备心跳', 'POST', '/device/heartbeat', {
    sn_code: TEST_DEVICE_ID,
    cpuUsage: 45.5,
    memoryUsage: 60.2,
    diskUsage: 50.0
  });

  await testAPI('设备状态查询', 'GET', '/device/status', {
    sn_code: TEST_DEVICE_ID
  });

  await testAPI('设备列表', 'GET', '/device/list', {
    pageNum: 1,
    pageSize: 10
  });

  // ============================================
  // 3. 简历管理
  // ============================================
  console.log('\n📋 【3/8】简历管理 API\n');

  const resumeResult = await testAPI('简历同步', 'POST', '/resume/sync', {
    sn_code: TEST_DEVICE_ID,
    platform: 'boss',
    resumeData: {
      fullName: '张三',
      gender: '男',
      age: 28,
      phone: '13800138000',
      email: 'test@example.com',
      location: '北京',
      education: '本科',
      major: '计算机科学',
      school: '清华大学',
      workYears: '5年',
      expectedPosition: '前端工程师',
      expectedSalary: '15-25K',
      skills: ['Vue', 'React', 'Node.js', 'TypeScript'],
      workExperience: [
        {
          company: 'XX科技公司',
          position: '高级前端工程师',
          duration: '2020-2024',
          description: '负责公司核心产品前端开发'
        }
      ]
    }
  });

  if (resumeResult.success && resumeResult.data?.data?.resumeId) {
    testResumeId = resumeResult.data.data.resumeId;
    console.log(`   💡 简历ID: ${testResumeId}`);
  }

  if (testResumeId) {
    await testAPI('获取简历详情', 'GET', '/resume/get', {
      resumeId: testResumeId
    });

    // AI分析需要API Key，可能失败
    console.log('   ⚠️  注意: AI分析需要配置API Key');
    await testAPI('简历AI分析', 'POST', '/resume/analyze', {
      resumeId: testResumeId
    });
  } else {
    printSkipped('获取简历详情', '简历创建失败');
    printSkipped('简历AI分析', '简历创建失败');
  }

  await testAPI('简历列表', 'GET', '/resume/list', {
    sn_code: TEST_DEVICE_ID,
    pageNum: 1,
    pageSize: 10
  });

  // ============================================
  // 4. 岗位管理
  // ============================================
  console.log('\n📋 【4/8】岗位管理 API\n');

  const jobResult = await testAPI('批量添加岗位', 'POST', '/job/batch-add', {
    sn_code: TEST_DEVICE_ID,
    platform: 'boss',
    jobs: [
      {
        platformJobId: 'job_001',
        jobTitle: '前端工程师',
        companyName: 'XX科技有限公司',
        companySize: '500-1000人',
        companyIndustry: '互联网',
        salary: '15-25K',
        salaryMin: 15,
        salaryMax: 25,
        location: '北京',
        experienceRequired: '3-5年',
        educationRequired: '本科',
        jobDescription: '负责公司产品前端开发，使用Vue/React技术栈',
        skillsRequired: 'Vue, React, JavaScript, TypeScript'
      },
      {
        platformJobId: 'job_002',
        jobTitle: 'Node.js工程师',
        companyName: 'YY互联网公司',
        salary: '20-30K',
        salaryMin: 20,
        salaryMax: 30,
        location: '北京',
        experienceRequired: '3-5年',
        educationRequired: '本科',
        jobDescription: '负责后端服务开发，使用Node.js技术栈',
        skillsRequired: 'Node.js, Express, MongoDB'
      }
    ]
  });

  if (jobResult.success && jobResult.data?.data?.createdCount > 0) {
    console.log(`   💡 成功创建 ${jobResult.data.data.createdCount} 个岗位`);
  }

  await testAPI('岗位列表', 'GET', '/job/list', {
    sn_code: TEST_DEVICE_ID,
    platform: 'boss',
    pageNum: 1,
    pageSize: 10
  });

  // ============================================
  // 5. 任务管理
  // ============================================
  console.log('\n📋 【5/8】任务管理 API\n');

  const taskResult = await testAPI('创建任务', 'POST', '/task/create', {
    sn_code: TEST_DEVICE_ID,
    taskType: 'job_search',
    taskName: '搜索前端岗位',
    priority: 5,
    taskData: {
      platform: 'boss',
      keyword: '前端工程师',
      location: '北京',
      limit: 50
    }
  });

  if (taskResult.success && taskResult.data?.data?.taskId) {
    testTaskId = taskResult.data.data.taskId;
    console.log(`   💡 任务ID: ${testTaskId}`);
  }

  if (testTaskId) {
    await testAPI('启动任务', 'POST', '/task/start', {
      taskId: testTaskId
    });

    await testAPI('更新任务进度', 'POST', '/task/progress', {
      taskId: testTaskId,
      progress: 50,
      message: '任务执行中'
    });

    await testAPI('查询任务状态', 'GET', '/task/status', {
      taskId: testTaskId
    });

    await testAPI('完成任务', 'POST', '/task/complete', {
      taskId: testTaskId,
      resultData: {
        jobsFound: 50,
        jobsFiltered: 20
      }
    });
  } else {
    printSkipped('启动任务', '任务创建失败');
    printSkipped('更新任务进度', '任务创建失败');
    printSkipped('查询任务状态', '任务创建失败');
    printSkipped('完成任务', '任务创建失败');
  }

  await testAPI('任务列表', 'GET', '/task/list', {
    sn_code: TEST_DEVICE_ID,
    pageNum: 1,
    pageSize: 10
  });

  // ============================================
  // 6. 聊天管理
  // ============================================
  console.log('\n📋 【6/8】聊天管理 API\n');

  // 需要先有岗位ID
  if (testJobId || jobResult.success) {
    await testAPI('添加聊天记录', 'POST', '/chat/add', {
      sn_code: TEST_DEVICE_ID,
      platform: 'boss',
      jobId: testJobId || 'job_001',
      chatType: 'greeting',
      direction: 'outgoing',
      content: '您好，我对贵公司的前端工程师岗位非常感兴趣',
      aiGenerated: true
    });
  } else {
    printSkipped('添加聊天记录', '无可用岗位ID');
  }

  await testAPI('聊天列表', 'GET', '/chat/list', {
    sn_code: TEST_DEVICE_ID,
    pageNum: 1,
    pageSize: 10
  });

  // ============================================
  // 7. 投递管理
  // ============================================
  console.log('\n📋 【7/8】投递管理 API\n');

  // 需要简历ID和岗位ID
  if (testResumeId && (testJobId || jobResult.success)) {
    await testAPI('投递记录', 'POST', '/apply/record', {
      sn_code: TEST_DEVICE_ID,
      platform: 'boss',
      jobId: testJobId || 'job_001',
      resumeId: testResumeId,
      applyMethod: 'online'
    });
  } else {
    printSkipped('投递记录', '缺少简历ID或岗位ID');
  }

  await testAPI('投递列表', 'GET', '/apply/list', {
    sn_code: TEST_DEVICE_ID,
    pageNum: 1,
    pageSize: 10
  });

  await testAPI('投递统计', 'GET', '/apply/statistics', {
    sn_code: TEST_DEVICE_ID,
    startDate: '2024-01-01',
    endDate: '2024-12-31'
  });

  // ============================================
  // 8. 清理测试数据（可选）
  // ============================================
  console.log('\n📋 【8/8】测试完成\n');

  // ============================================
  // 打印测试结果统计
  // ============================================
  console.log('\n' + '='.repeat(60));
  console.log('📊 测试结果统计');
  console.log('='.repeat(60));
  console.log(`总测试数: ${testResults.total}`);
  console.log(`✅ 通过: ${testResults.passed}`);
  console.log(`❌ 失败: ${testResults.failed}`);
  console.log(`⏭️  跳过: ${testResults.skipped}`);
  console.log(`成功率: ${((testResults.passed / testResults.total) * 100).toFixed(2)}%`);
  console.log('='.repeat(60));

  if (testResults.failed > 0) {
    console.log('\n⚠️  部分测试失败，可能原因:');
    console.log('  1. 后端服务未启动或未连接到数据库');
    console.log('  2. MQTT服务未启动');
    console.log('  3. AI API Key未配置');
    console.log('  4. 网络连接问题');
    console.log('\n💡 建议:');
    console.log('  - 检查后端服务是否正常运行');
    console.log('  - 查看后端服务日志');
    console.log('  - 确认数据库连接配置正确');
  } else if (testResults.failed === 0 && testResults.skipped === 0) {
    console.log('\n🎉 所有测试通过！系统运行正常！');
  }

  console.log('');
}

// 运行测试
console.log('正在连接到后端服务...\n');
setTimeout(() => {
  runTests().then(() => {
    process.exit(testResults.failed > 0 ? 1 : 0);
  }).catch(error => {
    console.warn('\n❌ 测试执行异常:', error.message);
    process.exit(1);
  });
}, 1000);
