const axios = require('axios');

// 测试有问题的政府端API接口
const baseURL = 'https://ad.ningmuyun.com';

const testAPIs = [
  // 正常的接口
  {
    name: '数据览仓接口',
    url: '/api/government/data-center',
    method: 'GET',
    category: '正常'
  },
  {
    name: '市场行情接口',
    url: '/api/government/market-price',
    method: 'GET',
    category: '正常'
  },
  {
    name: '行政部门接口',
    url: '/api/government/departments',
    method: 'GET',
    category: '正常'
  },
  
  // 有问题的接口
  {
    name: '行政人员接口',
    url: '/api/government/admin-staff',
    method: 'GET',
    category: '问题'
  },
  {
    name: '养殖户接口',
    url: '/api/government/farmers',
    method: 'GET',
    category: '问题'
  },
  {
    name: '智能项圈接口',
    url: '/api/government/collars',
    method: 'GET',
    category: '问题'
  },
  {
    name: '养殖类型接口',
    url: '/api/government/farm-types',
    method: 'GET',
    category: '问题'
  },
  {
    name: '养殖种类接口',
    url: '/api/government/animal-types',
    method: 'GET',
    category: '问题'
  }
];

async function testAPI(api) {
  try {
    console.log(`\n测试 ${api.name} (${api.category})...`);
    console.log(`URL: ${baseURL}${api.url}`);
    
    const response = await axios({
      method: api.method,
      url: `${baseURL}${api.url}`,
      timeout: 10000,
      headers: {
        'Content-Type': 'application/json'
      }
    });
    
    console.log(`✅ 状态码: ${response.status}`);
    console.log(`✅ 响应: ${JSON.stringify(response.data).substring(0, 100)}...`);
    return { success: true, status: response.status, data: response.data };
    
  } catch (error) {
    console.log(`❌ 错误: ${error.message}`);
    if (error.response) {
      console.log(`❌ 状态码: ${error.response.status}`);
      console.log(`❌ 响应: ${JSON.stringify(error.response.data)}`);
      
      // 检查是否是路径重复错误
      if (error.response.data && JSON.stringify(error.response.data).includes('government/government')) {
        console.log(`🚨 发现路径重复错误！`);
      }
      
      // 检查是否是404错误
      if (error.response.status === 404) {
        console.log(`🚨 404错误 - 接口不存在或路由配置问题`);
      }
    } else if (error.code === 'ECONNREFUSED') {
      console.log(`🚨 连接被拒绝 - 服务可能没有启动`);
    }
    return { success: false, error: error.message, status: error.response?.status };
  }
}

async function runTests() {
  console.log('测试政府端API接口 - 对比正常和问题接口...');
  console.log('='.repeat(60));
  
  const results = [];
  
  for (const api of testAPIs) {
    const result = await testAPI(api);
    results.push({
      name: api.name,
      url: api.url,
      category: api.category,
      ...result
    });
    
    // 等待1秒再测试下一个接口
    await new Promise(resolve => setTimeout(resolve, 1000));
  }
  
  console.log('\n' + '='.repeat(60));
  console.log('测试结果汇总:');
  console.log('='.repeat(60));
  
  // 按类别分组统计
  const normalAPIs = results.filter(r => r.category === '正常');
  const problemAPIs = results.filter(r => r.category === '问题');
  
  console.log('\n正常接口:');
  normalAPIs.forEach(result => {
    const status = result.success ? '✅' : '❌';
    console.log(`  ${status} ${result.name}`);
  });
  
  console.log('\n问题接口:');
  problemAPIs.forEach(result => {
    const status = result.success ? '✅' : '❌';
    console.log(`  ${status} ${result.name}`);
  });
  
  const normalSuccess = normalAPIs.filter(r => r.success).length;
  const problemSuccess = problemAPIs.filter(r => r.success).length;
  
  console.log(`\n统计结果:`);
  console.log(`正常接口: ${normalSuccess}/${normalAPIs.length} 通过`);
  console.log(`问题接口: ${problemSuccess}/${problemAPIs.length} 通过`);
  
  if (normalSuccess === normalAPIs.length && problemSuccess === 0) {
    console.log('\n🔍 分析结果:');
    console.log('✅ 正常接口全部通过');
    console.log('❌ 问题接口全部失败');
    console.log('🚨 这表明问题接口确实存在路由或实现问题');
  } else if (normalSuccess === normalAPIs.length && problemSuccess > 0) {
    console.log('\n🔍 分析结果:');
    console.log('✅ 部分问题接口已修复');
    console.log('⚠️  仍有部分问题接口需要修复');
  } else {
    console.log('\n🔍 分析结果:');
    console.log('⚠️  所有接口都有问题，可能是nginx配置问题');
  }
}

// 运行测试
runTests().catch(console.error);
