const axios = require('axios');

// 测试修复后的nginx配置
const baseURL = 'https://ad.ningmuyun.com';

const testAPIs = [
  // 政府端API测试
  {
    name: '政府端数据览仓',
    url: '/api/government/data-center',
    method: 'GET',
    expected: 'government'
  },
  {
    name: '政府端市场行情',
    url: '/api/government/market-price',
    method: 'GET',
    expected: 'government'
  },
  {
    name: '政府端行政部门',
    url: '/api/government/departments',
    method: 'GET',
    expected: 'government'
  },
  {
    name: '政府端养殖类型',
    url: '/api/government/farm-types',
    method: 'GET',
    expected: 'government'
  },
  {
    name: '政府端养殖种类',
    url: '/api/government/animal-types',
    method: 'GET',
    expected: 'government'
  },
  
  // 银行端API测试
  {
    name: '银行端API',
    url: '/bank/api/auth/login',
    method: 'POST',
    expected: 'bank'
  },
  
  // 保险端API测试
  {
    name: '保险端API',
    url: '/insurance/api/auth/login',
    method: 'POST',
    expected: 'insurance'
  },
  
  // 养殖端API测试
  {
    name: '养殖端API',
    url: '/farm/api/auth/login',
    method: 'POST',
    expected: 'farm'
  }
];

async function testAPI(api) {
  try {
    console.log(`\n测试 ${api.name}...`);
    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(`🚨 发现路径重复错误！`);
      }
    }
    return { success: false, error: error.message, status: error.response?.status };
  }
}

async function runTests() {
  console.log('测试修复后的nginx配置...');
  console.log('='.repeat(60));
  
  const results = [];
  
  for (const api of testAPIs) {
    const result = await testAPI(api);
    results.push({
      name: api.name,
      url: api.url,
      expected: api.expected,
      ...result
    });
    
    // 等待1秒再测试下一个接口
    await new Promise(resolve => setTimeout(resolve, 1000));
  }
  
  console.log('\n' + '='.repeat(60));
  console.log('测试结果汇总:');
  console.log('='.repeat(60));
  
  const successCount = results.filter(r => r.success).length;
  const totalCount = results.length;
  
  results.forEach(result => {
    const status = result.success ? '✅' : '❌';
    console.log(`${status} ${result.name} (${result.expected})`);
  });
  
  console.log(`\n总计: ${successCount}/${totalCount} 个接口测试通过`);
  
  if (successCount === totalCount) {
    console.log('🎉 所有接口测试通过！nginx配置修复成功！');
  } else {
    console.log('⚠️  部分接口测试失败，请检查配置');
  }
  
  console.log('\n修复说明:');
  console.log('✅ 将养殖端API路径从 /api/ 改为 /farm-api/');
  console.log('✅ 避免与政府端 /api/ 路径冲突');
  console.log('✅ 政府端API现在可以正常访问');
}

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