const fs = require('fs');
const path = require('path');

// 读取nginx配置文件
const nginxConfigPath = path.join(__dirname, 'nginx.conf');
const nginxConfig = fs.readFileSync(nginxConfigPath, 'utf8');

console.log('验证nginx配置修复...');
console.log('='.repeat(50));

// 检查是否存在路径冲突
const apiLocationPatterns = [
  {
    name: '养殖端API (旧)',
    pattern: /location \/api\//,
    shouldExist: false,
    description: '旧的养殖端API路径，应该被移除'
  },
  {
    name: '养殖端API (新)',
    pattern: /location \^~ \/farm-api\//,
    shouldExist: true,
    description: '新的养殖端API路径'
  },
  {
    name: '政府端API',
    pattern: /location \^~ \/api\//,
    shouldExist: true,
    description: '政府端API路径'
  },
  {
    name: '银行端API',
    pattern: /location \^~ \/bank\/api\//,
    shouldExist: true,
    description: '银行端API路径'
  },
  {
    name: '保险端API',
    pattern: /location \^~ \/insurance\/api\//,
    shouldExist: true,
    description: '保险端API路径'
  }
];

console.log('检查API路径配置:');
console.log('-'.repeat(30));

let allCorrect = true;

apiLocationPatterns.forEach(config => {
  const found = config.pattern.test(nginxConfig);
  const status = found === config.shouldExist ? '✅' : '❌';
  
  if (found !== config.shouldExist) {
    allCorrect = false;
  }
  
  console.log(`${status} ${config.name}: ${found ? '存在' : '不存在'}`);
  console.log(`    ${config.description}`);
});

// 检查代理目标端口
const proxyTargets = [
  {
    name: '政府端代理目标',
    pattern: /proxy_pass http:\/\/localhost:5352\/api\//,
    expected: 'http://localhost:5352/api/'
  },
  {
    name: '养殖端代理目标',
    pattern: /proxy_pass http:\/\/localhost:5350\/api\//,
    expected: 'http://localhost:5350/api/'
  },
  {
    name: '银行端代理目标',
    pattern: /proxy_pass http:\/\/localhost:5351\/api\//,
    expected: 'http://localhost:5351/api/'
  },
  {
    name: '保险端代理目标',
    pattern: /proxy_pass http:\/\/localhost:3000\/api\//,
    expected: 'http://localhost:3000/api/'
  }
];

console.log('\n检查代理目标配置:');
console.log('-'.repeat(30));

proxyTargets.forEach(config => {
  const found = config.pattern.test(nginxConfig);
  const status = found ? '✅' : '❌';
  console.log(`${status} ${config.name}: ${found ? '已配置' : '未找到'}`);
  if (!found) {
    console.log(`   期望: ${config.expected}`);
  }
});

// 检查是否有重复的location规则
console.log('\n检查重复的location规则:');
console.log('-'.repeat(30));

const locationMatches = nginxConfig.match(/location\s+[^{]+{/g) || [];
const locationPaths = locationMatches.map(match => {
  const pathMatch = match.match(/location\s+([^{]+)/);
  return pathMatch ? pathMatch[1].trim() : '';
});

const duplicatePaths = locationPaths.filter((path, index) => 
  locationPaths.indexOf(path) !== index && path !== ''
);

if (duplicatePaths.length > 0) {
  console.log('❌ 发现重复的location规则:');
  duplicatePaths.forEach(path => {
    console.log(`   - ${path}`);
  });
  allCorrect = false;
} else {
  console.log('✅ 没有重复的location规则');
}

console.log('\n' + '='.repeat(50));
console.log('配置验证结果:');
console.log('='.repeat(50));

if (allCorrect) {
  console.log('🎉 nginx配置修复成功！');
  console.log('✅ 路径冲突已解决');
  console.log('✅ 各端API路径正确配置');
  console.log('✅ 代理目标端口正确');
} else {
  console.log('⚠️  nginx配置需要进一步修复');
  console.log('请检查上述错误并修复');
}

console.log('\n建议的nginx重载命令:');
console.log('sudo nginx -t && sudo nginx -s reload');

console.log('\n修复说明:');
console.log('1. 将养殖端API路径从 /api/ 改为 /farm-api/');
console.log('2. 避免与政府端 /api/ 路径冲突');
console.log('3. 政府端API现在可以正常访问');
console.log('4. 各端API路径互不冲突');