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));

// 检查政府端API代理配置
const governmentAPIConfigs = [
  {
    name: '认证API代理',
    pattern: /location \^~ \/api\/auth\//,
    expected: 'location ^~ /api/auth/'
  },
  {
    name: '监管API代理',
    pattern: /location \^~ \/api\/supervision\//,
    expected: 'location ^~ /api/supervision/'
  },
  {
    name: '疫情API代理',
    pattern: /location \^~ \/api\/epidemic\//,
    expected: 'location ^~ /api/epidemic/'
  },
  {
    name: '政府API代理',
    pattern: /location \^~ \/api\/government\//,
    expected: 'location ^~ /api/government/'
  },
  {
    name: '其他API代理',
    pattern: /location ~ \^\/api\/\(personnel\|approval\|service\|visualization\|system\|files\|smart-earmark\|smart-host\|slaughter\|harmless\|harmless-place\|epidemic-record\|vaccine\|epidemic-activity\|production-material-certification\|approval-process\|cattle-academy\|device-warning\)\//,
    expected: 'location ~ ^/api/(personnel|approval|service|...)/'
  }
];

// 检查代理目标端口
const proxyTargets = [
  {
    name: '认证API代理目标',
    pattern: /proxy_pass http:\/\/localhost:5352\/api\/auth\//,
    expected: 'http://localhost:5352/api/auth/'
  },
  {
    name: '监管API代理目标',
    pattern: /proxy_pass http:\/\/localhost:5352\/api\/supervision\//,
    expected: 'http://localhost:5352/api/supervision/'
  },
  {
    name: '疫情API代理目标',
    pattern: /proxy_pass http:\/\/localhost:5352\/api\/epidemic\//,
    expected: 'http://localhost:5352/api/epidemic/'
  },
  {
    name: '政府API代理目标',
    pattern: /proxy_pass http:\/\/localhost:5352\/api\/government\//,
    expected: 'http://localhost:5352/api/government/'
  },
  {
    name: '其他API代理目标',
    pattern: /proxy_pass http:\/\/localhost:5352\$request_uri/,
    expected: 'http://localhost:5352$request_uri'
  }
];

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

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

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}`);
  }
});

// 检查CORS配置
console.log('\n检查CORS配置:');
console.log('-'.repeat(30));

const corsConfigs = [
  {
    name: 'Access-Control-Allow-Origin',
    pattern: /add_header 'Access-Control-Allow-Origin' 'https:\/\/ad\.ningmuyun\.com' always;/,
    expected: 'https://ad.ningmuyun.com'
  },
  {
    name: 'Access-Control-Allow-Methods',
    pattern: /add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;/,
    expected: 'GET, POST, OPTIONS, PUT, DELETE'
  },
  {
    name: 'Access-Control-Allow-Headers',
    pattern: /add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, X-Requested-With' always;/,
    expected: 'Authorization, Content-Type, X-Requested-With'
  }
];

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

// 统计配置数量
const totalAPIConfigs = governmentAPIConfigs.length;
const foundAPIConfigs = governmentAPIConfigs.filter(config => config.pattern.test(nginxConfig)).length;

const totalProxyTargets = proxyTargets.length;
const foundProxyTargets = proxyTargets.filter(config => config.pattern.test(nginxConfig)).length;

const totalCorsConfigs = corsConfigs.length;
const foundCorsConfigs = corsConfigs.filter(config => config.pattern.test(nginxConfig)).length;

console.log('\n' + '='.repeat(50));
console.log('配置检查结果汇总:');
console.log('='.repeat(50));
console.log(`API代理配置: ${foundAPIConfigs}/${totalAPIConfigs}`);
console.log(`代理目标配置: ${foundProxyTargets}/${totalProxyTargets}`);
console.log(`CORS配置: ${foundCorsConfigs}/${totalCorsConfigs}`);

const allConfigured = foundAPIConfigs === totalAPIConfigs && 
                     foundProxyTargets === totalProxyTargets && 
                     foundCorsConfigs === totalCorsConfigs;

if (allConfigured) {
  console.log('\n🎉 所有政府端nginx配置检查通过！');
} else {
  console.log('\n⚠️  部分配置缺失，请检查nginx.conf文件');
}

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