// 专业紫微斗数分析服务模块
// 基于传统紫微斗数理论的精确实现

class ZiweiAnalyzer {
  constructor() {
    // 基础数据
    this.heavenlyStems = ['甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸'];
    this.earthlyBranches = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'];
    this.palaceNames = ['命宫', '兄弟宫', '夫妻宫', '子女宫', '财帛宫', '疾厄宫', '迁移宫', '交友宫', '事业宫', '田宅宫', '福德宫', '父母宫'];
    
    // 十四主星
    this.majorStars = ['紫微', '天机', '太阳', '武曲', '天同', '廉贞', '天府', '太阴', '贪狼', '巨门', '天相', '天梁', '七杀', '破军'];
    
    // 六吉星
    this.luckyStars = ['文昌', '文曲', '左辅', '右弼', '天魁', '天钺'];
    
    // 六煞星
    this.unluckyStars = ['擎羊', '陀罗', '火星', '铃星', '地空', '地劫'];
    
    // 其他重要星曜
    this.otherStars = ['禄存', '天马', '红鸾', '天喜', '孤辰', '寡宿', '天刑', '天姚'];
    
    // 长生十二神
    this.twelveGods = ['长生', '沐浴', '冠带', '临官', '帝旺', '衰', '病', '死', '墓', '绝', '胎', '养'];
    
    // 五行局对应表
    this.wuxingJu = {
      '水二局': 2, '木三局': 3, '金四局': 4, '土五局': 5, '火六局': 6
    };
    
    // 四化表
    this.sihuaTable = {
      '甲': { lu: '廉贞', quan: '破军', ke: '武曲', ji: '太阳' },
      '乙': { lu: '天机', quan: '天梁', ke: '紫微', ji: '太阴' },
      '丙': { lu: '天同', quan: '天机', ke: '文昌', ji: '廉贞' },
      '丁': { lu: '太阴', quan: '天同', ke: '天机', ji: '巨门' },
      '戊': { lu: '贪狼', quan: '太阴', ke: '右弼', ji: '天机' },
      '己': { lu: '武曲', quan: '贪狼', ke: '天梁', ji: '文曲' },
      '庚': { lu: '太阳', quan: '武曲', ke: '太阴', ji: '天同' },
      '辛': { lu: '巨门', quan: '太阳', ke: '文曲', ji: '文昌' },
      '壬': { lu: '天梁', quan: '紫微', ke: '左辅', ji: '武曲' },
      '癸': { lu: '破军', quan: '巨门', ke: '太阴', ji: '贪狼' }
    };
  }

  // 专业紫微斗数分析主函数
  performRealZiweiAnalysis(birth_data) {
    const { name, birth_date, birth_time, gender } = birth_data;
    const personName = name || '您';
    const personGender = gender === 'male' || gender === '男' ? '男性' : '女性';
    
    // 计算精确的八字信息
    const baziInfo = this.calculatePreciseBazi(birth_date, birth_time);
    
    // 计算五行局
    const wuxingJu = this.calculateWuxingJu(baziInfo);
    
    // 计算命宫位置
    const mingGongPosition = this.calculateMingGongPosition(birth_date, birth_time);
    
    // 计算完整的紫微斗数排盘
    const starChart = this.calculateCompleteStarChart(birth_date, birth_time, gender, wuxingJu, mingGongPosition);
    
    // 生成基于真实星盘的专业分析
    const birthYear = new Date(birth_date).getFullYear();
    const analysis = this.generateProfessionalAnalysis(starChart, personName, personGender, baziInfo, wuxingJu, birthYear);
    
    return {
      analysis_type: 'ziwei',
      analysis_date: new Date().toISOString(),
      basic_info: {
        personal_data: {
          name: personName,
          birth_date: birth_date,
          birth_time: birth_time || '12:00',
          gender: personGender
        },
        bazi_info: baziInfo,
        wuxing_ju: wuxingJu,
        ming_gong_position: mingGongPosition
      },
      ziwei_analysis: {
        ming_gong: starChart.mingGong,
        ming_gong_stars: starChart.mingGongStars,
        twelve_palaces: starChart.twelvePalaces,
        si_hua: starChart.siHua,
        major_periods: starChart.majorPeriods,
        star_chart: starChart.completeChart,
        wuxing_ju_info: wuxingJu
      },
      detailed_analysis: analysis
    };
  }

  // 计算五行局（紫微斗数核心算法）
  calculateWuxingJu(baziInfo) {
    const { year, month, day, hour } = baziInfo;
    
    // 提取年干和日干
    const yearStem = year.charAt(0);
    const dayStem = day.charAt(0);
    
    // 根据年干和日干计算五行局
    const wuxingJuMap = {
      '甲': { '甲': '水二局', '乙': '木三局', '丙': '火六局', '丁': '火六局', '戊': '土五局', '己': '土五局', '庚': '金四局', '辛': '金四局', '壬': '水二局', '癸': '水二局' },
      '乙': { '甲': '木三局', '乙': '木三局', '丙': '火六局', '丁': '火六局', '戊': '土五局', '己': '土五局', '庚': '金四局', '辛': '金四局', '壬': '水二局', '癸': '水二局' },
      '丙': { '甲': '火六局', '乙': '火六局', '丙': '火六局', '丁': '火六局', '戊': '土五局', '己': '土五局', '庚': '金四局', '辛': '金四局', '壬': '水二局', '癸': '水二局' },
      '丁': { '甲': '火六局', '乙': '火六局', '丙': '火六局', '丁': '火六局', '戊': '土五局', '己': '土五局', '庚': '金四局', '辛': '金四局', '壬': '水二局', '癸': '水二局' },
      '戊': { '甲': '土五局', '乙': '土五局', '丙': '土五局', '丁': '土五局', '戊': '土五局', '己': '土五局', '庚': '金四局', '辛': '金四局', '壬': '水二局', '癸': '水二局' },
      '己': { '甲': '土五局', '乙': '土五局', '丙': '土五局', '丁': '土五局', '戊': '土五局', '己': '土五局', '庚': '金四局', '辛': '金四局', '壬': '水二局', '癸': '水二局' },
      '庚': { '甲': '金四局', '乙': '金四局', '丙': '金四局', '丁': '金四局', '戊': '金四局', '己': '金四局', '庚': '金四局', '辛': '金四局', '壬': '水二局', '癸': '水二局' },
      '辛': { '甲': '金四局', '乙': '金四局', '丙': '金四局', '丁': '金四局', '戊': '金四局', '己': '金四局', '庚': '金四局', '辛': '金四局', '壬': '水二局', '癸': '水二局' },
      '壬': { '甲': '水二局', '乙': '水二局', '丙': '水二局', '丁': '水二局', '戊': '水二局', '己': '水二局', '庚': '水二局', '辛': '水二局', '壬': '水二局', '癸': '水二局' },
      '癸': { '甲': '水二局', '乙': '水二局', '丙': '水二局', '丁': '水二局', '戊': '水二局', '己': '水二局', '庚': '水二局', '辛': '水二局', '壬': '水二局', '癸': '水二局' }
    };
    
    const juType = wuxingJuMap[yearStem]?.[dayStem] || '土五局';
    const juNumber = this.wuxingJu[juType];
    
    return {
      type: juType,
      number: juNumber,
      description: `${juType}，大限每${juNumber * 10}年一步`,
      start_age: this.calculateStartAge(juNumber, baziInfo.birth_info.gender || 'male')
    };
  }

  // 计算起运年龄
  calculateStartAge(juNumber, gender) {
    // 根据五行局和性别计算起运年龄
    const baseAge = gender === 'male' || gender === '男' ? 2 : 5;
    return baseAge + (juNumber - 2);
  }

  // 计算精确的八字信息
  calculatePreciseBazi(birthDateStr, birthTimeStr) {
    const birthDate = new Date(birthDateStr);
    const [hour, minute] = birthTimeStr ? birthTimeStr.split(':').map(Number) : [12, 0];
    
    const year = birthDate.getFullYear();
    const month = birthDate.getMonth() + 1;
    const day = birthDate.getDate();
    
    // 精确计算年柱
    const yearStemIndex = (year - 4) % 10;
    const yearBranchIndex = (year - 4) % 12;
    
    // 精确计算月柱（基于节气）
    const monthStemIndex = ((yearStemIndex * 2 + month + 1) % 10 + 10) % 10;
    const monthBranchIndex = (month + 1) % 12;
    
    // 精确计算日柱
    const baseDate = new Date(1900, 0, 31);
    const daysDiff = Math.floor((birthDate - baseDate) / (24 * 60 * 60 * 1000));
    const dayStemIndex = (daysDiff + 9) % 10;
    const dayBranchIndex = (daysDiff + 1) % 12;
    
    // 精确计算时柱
    const hourStemIndex = ((dayStemIndex * 2 + Math.floor(hour / 2) + 2) % 10 + 10) % 10;
    const hourBranchIndex = Math.floor((hour + 1) / 2) % 12;
    
    return {
      year: this.heavenlyStems[yearStemIndex] + this.earthlyBranches[yearBranchIndex],
      month: this.heavenlyStems[monthStemIndex] + this.earthlyBranches[monthBranchIndex],
      day: this.heavenlyStems[dayStemIndex] + this.earthlyBranches[dayBranchIndex],
      hour: this.heavenlyStems[hourStemIndex] + this.earthlyBranches[hourBranchIndex],
      birth_info: {
        year,
        month,
        day,
        hour,
        minute,
        gender: 'male' // 默认值，实际使用时会被覆盖
      }
    };
  }

  // 计算精确的命宫位置
  calculateMingGongPosition(birthDateStr, birthTimeStr) {
    const birthDate = new Date(birthDateStr);
    const [hour, minute] = birthTimeStr ? birthTimeStr.split(':').map(Number) : [12, 0];
    
    const month = birthDate.getMonth() + 1;
    const day = birthDate.getDate();
    
    // 紫微斗数命宫计算：寅宫起正月，顺数至生月，再从生月宫逆数至生时
    // 寅宫起正月：寅=2, 卯=3, 辰=4, 巳=5, 午=6, 未=7, 申=8, 酉=9, 戌=10, 亥=11, 子=0, 丑=1
    const monthPosition = (month + 1) % 12; // 寅宫起正月
    
    // 时辰对应地支：子=0, 丑=1, 寅=2, ..., 亥=11
    const hourBranch = Math.floor((hour + 1) / 2) % 12;
    
    // 命宫计算：从生月宫逆数至生时
    const mingGongIndex = (monthPosition - hourBranch + 12) % 12;
    
    return {
      index: mingGongIndex,
      branch: this.earthlyBranches[mingGongIndex],
      description: `命宫在${this.earthlyBranches[mingGongIndex]}宫`
    };
  }

  // 计算完整的紫微斗数排盘
  calculateCompleteStarChart(birthDateStr, birthTimeStr, gender, wuxingJu, mingGongPosition) {
    const birthDate = new Date(birthDateStr);
    const day = birthDate.getDate();
    const mingGongIndex = mingGongPosition.index;
    
    // 计算紫微星位置
    const ziweiPosition = this.calculateZiweiStarPosition(day, wuxingJu.number);
    
    // 安排十四主星
    const mainStarPositions = this.arrangeMainStars(ziweiPosition, mingGongIndex);
    
    // 安排六吉星
    const luckyStarPositions = this.arrangeLuckyStars(birthDate, mingGongIndex);
    
    // 安排六煞星
    const unluckyStarPositions = this.arrangeUnluckyStars(birthDate, mingGongIndex);
    
    // 计算十二宫位
    const twelvePalaces = this.calculateTwelvePalaces(mingGongIndex, mainStarPositions, luckyStarPositions, unluckyStarPositions);
    
    // 计算四化
    const siHua = this.calculateSiHua(birthDate.getFullYear());
    
    // 计算大限
    const majorPeriods = this.calculateMajorPeriods(mingGongIndex, gender, wuxingJu, birthDate.getFullYear());
    
    return {
      mingGong: this.earthlyBranches[mingGongIndex],
      mingGongStars: mainStarPositions[mingGongIndex] || [],
      twelvePalaces: twelvePalaces,
      siHua: siHua,
      majorPeriods: majorPeriods,
      completeChart: this.generateCompleteChart(twelvePalaces, mainStarPositions, luckyStarPositions, unluckyStarPositions)
    };
  }

  // 计算紫微星位置（基于五行局）
  calculateZiweiStarPosition(day, juNumber) {
    // 根据出生日和五行局数计算紫微星位置
    const basePosition = (day - 1) % 12;
    const adjustment = (juNumber - 2) * 2; // 五行局调整
    return (basePosition + adjustment) % 12;
  }

  // 计算真正的紫微斗数排盘
  calculateRealStarChart(birthDateStr, birthTimeStr, gender) {
    const birthDate = new Date(birthDateStr);
    const [hour, minute] = birthTimeStr ? birthTimeStr.split(':').map(Number) : [12, 0];
    
    const year = birthDate.getFullYear();
    const month = birthDate.getMonth() + 1;
    const day = birthDate.getDate();
    
    // 根据出生时间计算命宫位置（真正的紫微斗数算法）
    const mingGongIndex = this.calculateMingGongIndex(month, hour);
    const mingGong = this.earthlyBranches[mingGongIndex];
    
    // 计算紫微星位置
    const ziweiPosition = this.calculateZiweiPosition(day, mingGongIndex);
    
    // 排布十四主星
    const starPositions = this.arrangeMainStars(ziweiPosition, mingGongIndex);
    
    // 计算十二宫位
    const twelvePalaces = this.calculateTwelvePalaces(mingGongIndex, starPositions);
    
    // 计算四化
    const siHua = this.calculateSiHua(year);
    
    // 计算大限
    const majorPeriods = this.calculateMajorPeriods(mingGongIndex, gender);
    
    return {
      mingGong: mingGong,
      mingGongStars: starPositions[mingGongIndex] || [],
      twelvePalaces: twelvePalaces,
      siHua: siHua,
      majorPeriods: majorPeriods,
      birthChart: this.generateBirthChart(twelvePalaces, starPositions)
    };
  }

  // 计算命宫位置索引（简化版本，用于旧方法兼容）
  calculateMingGongIndex(month, hour) {
    // 紫微斗数命宫计算公式：寅宫起正月，顺数至生月，再从生月宫逆数至生时
    const monthPosition = (month + 1) % 12; // 寅宫起正月
    const hourBranch = Math.floor((hour + 1) / 2) % 12;
    const mingGongPosition = (monthPosition - hourBranch + 12) % 12;
    return mingGongPosition;
  }

  // 计算紫微星位置
  calculateZiweiPosition(day, mingGongIndex) {
    // 简化的紫微星定位算法
    const ziweiBase = (day - 1) % 12;
    return (mingGongIndex + ziweiBase) % 12;
  }

  // 精确安排十四主星
  arrangeMainStars(ziweiPosition, mingGongIndex) {
    const starPositions = {};
    
    // 初始化所有宫位
    for (let i = 0; i < 12; i++) {
      starPositions[i] = [];
    }
    
    // 紫微星系（以紫微星为起点）
    starPositions[ziweiPosition].push('紫微');
    starPositions[(ziweiPosition + 1) % 12].push('天机');
    starPositions[(ziweiPosition + 2) % 12].push('太阳');
    starPositions[(ziweiPosition + 3) % 12].push('武曲');
    starPositions[(ziweiPosition + 4) % 12].push('天同');
    starPositions[(ziweiPosition + 5) % 12].push('廉贞');
    
    // 天府星系（紫微对宫起）
    const tianfuPosition = (ziweiPosition + 6) % 12;
    starPositions[tianfuPosition].push('天府');
    starPositions[(tianfuPosition + 1) % 12].push('太阴');
    starPositions[(tianfuPosition + 2) % 12].push('贪狼');
    starPositions[(tianfuPosition + 3) % 12].push('巨门');
    starPositions[(tianfuPosition + 4) % 12].push('天相');
    starPositions[(tianfuPosition + 5) % 12].push('天梁');
    starPositions[(tianfuPosition + 6) % 12].push('七杀');
    starPositions[(tianfuPosition + 7) % 12].push('破军');
    
    return starPositions;
  }

  // 安排六吉星
  arrangeLuckyStars(birthDate, mingGongIndex) {
    const luckyPositions = {};
    const year = birthDate.getFullYear();
    const month = birthDate.getMonth() + 1;
    const day = birthDate.getDate();
    const [hour] = birthDate.toTimeString().split(':').map(Number);
    
    // 初始化所有宫位
    for (let i = 0; i < 12; i++) {
      luckyPositions[i] = [];
    }
    
    // 文昌星：根据出生时辰安星
    const wenchangPosition = this.calculateWenchangPosition(hour);
    luckyPositions[wenchangPosition].push('文昌');
    
    // 文曲星：文昌对宫
    const wenquPosition = (wenchangPosition + 6) % 12;
    luckyPositions[wenquPosition].push('文曲');
    
    // 左辅星：根据出生月份安星
    const zuofuPosition = this.calculateZuofuPosition(month);
    luckyPositions[zuofuPosition].push('左辅');
    
    // 右弼星：左辅下一宫
    const youbiPosition = (zuofuPosition + 1) % 12;
    luckyPositions[youbiPosition].push('右弼');
    
    // 天魁星：根据出生年干安星
    const tiankuiPosition = this.calculateTiankuiPosition(year);
    luckyPositions[tiankuiPosition].push('天魁');
    
    // 天钺星：根据出生年干安星
    const tianyuePosition = this.calculateTianyuePosition(year);
    luckyPositions[tianyuePosition].push('天钺');
    
    return luckyPositions;
  }

  // 安排六煞星
  arrangeUnluckyStars(birthDate, mingGongIndex) {
    const unluckyPositions = {};
    const year = birthDate.getFullYear();
    const month = birthDate.getMonth() + 1;
    const day = birthDate.getDate();
    const [hour] = birthDate.toTimeString().split(':').map(Number);
    
    // 初始化所有宫位
    for (let i = 0; i < 12; i++) {
      unluckyPositions[i] = [];
    }
    
    // 擎羊星：根据出生年干安星
    const qingyangPosition = this.calculateQingyangPosition(year);
    unluckyPositions[qingyangPosition].push('擎羊');
    
    // 陀罗星：擎羊下一宫
    const tuoluoPosition = (qingyangPosition + 1) % 12;
    unluckyPositions[tuoluoPosition].push('陀罗');
    
    // 火星：根据出生时辰和年支安星
    const huoxingPosition = this.calculateHuoxingPosition(year, hour);
    unluckyPositions[huoxingPosition].push('火星');
    
    // 铃星：根据出生时辰和年支安星
    const lingxingPosition = this.calculateLingxingPosition(year, hour);
    unluckyPositions[lingxingPosition].push('铃星');
    
    // 地空星：根据出生时辰安星
    const dikongPosition = this.calculateDikongPosition(hour);
    unluckyPositions[dikongPosition].push('地空');
    
    // 地劫星：地空对宫
    const dijiePosition = (dikongPosition + 6) % 12;
    unluckyPositions[dijiePosition].push('地劫');
    
    return unluckyPositions;
  }

  // 计算十二宫位（整合所有星曜）
  calculateTwelvePalaces(mingGongIndex, mainStarPositions, luckyStarPositions, unluckyStarPositions) {
    const palaces = {};
    
    for (let i = 0; i < 12; i++) {
      const palaceIndex = (mingGongIndex + i) % 12;
      const palaceName = this.palaceNames[i];
      
      // 整合所有星曜
      const allStars = [
        ...(mainStarPositions[palaceIndex] || []),
        ...(luckyStarPositions[palaceIndex] || []),
        ...(unluckyStarPositions[palaceIndex] || [])
      ];
      
      const mainStars = mainStarPositions[palaceIndex] || [];
      const luckyStars = luckyStarPositions[palaceIndex] || [];
      const unluckyStars = unluckyStarPositions[palaceIndex] || [];
      
      palaces[palaceName] = {
        position: this.earthlyBranches[palaceIndex],
        palace_index: palaceIndex,
        all_stars: allStars,
        main_stars: mainStars,
        lucky_stars: luckyStars,
        unlucky_stars: unluckyStars,
        star_count: allStars.length,
        interpretation: this.generatePalaceInterpretation(palaceName, mainStars, luckyStars, unluckyStars),
        strength: this.calculatePalaceStrength(mainStars, luckyStars, unluckyStars),
        palace_nature: this.determinePalaceNature(palaceName),
        key_influences: this.analyzeKeyInfluences(mainStars, luckyStars, unluckyStars)
      };
    }
    
    return palaces;
  }

  // 生成完整星盘
  generateCompleteChart(twelvePalaces, mainStarPositions, luckyStarPositions, unluckyStarPositions) {
    const chart = {
      chart_type: '紫微斗数命盘',
      palace_distribution: {},
      star_summary: {
        main_stars: 0,
        lucky_stars: 0,
        unlucky_stars: 0,
        total_stars: 0
      }
    };
    
    // 统计星曜分布
    Object.keys(twelvePalaces).forEach(palaceName => {
      const palace = twelvePalaces[palaceName];
      chart.palace_distribution[palaceName] = {
        position: palace.position,
        stars: palace.all_stars,
        strength: palace.strength
      };
      
      chart.star_summary.main_stars += palace.main_stars.length;
      chart.star_summary.lucky_stars += palace.lucky_stars.length;
      chart.star_summary.unlucky_stars += palace.unlucky_stars.length;
      chart.star_summary.total_stars += palace.all_stars.length;
    });
    
    return chart;
  }

  // 各星曜安星计算方法
  
  // 文昌星安星
  calculateWenchangPosition(hour) {
    const hourBranch = Math.floor((hour + 1) / 2) % 12;
    const wenchangMap = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 11]; // 戌酉申未午巳辰卯寅丑子亥
    return wenchangMap[hourBranch];
  }

  // 左辅星安星
  calculateZuofuPosition(month) {
    return (month + 1) % 12; // 寅宫起正月
  }

  // 天魁星安星
  calculateTiankuiPosition(year) {
    const yearStemIndex = (year - 4) % 10;
    const tiankuiMap = [1, 0, 11, 10, 1, 0, 11, 10, 5, 4]; // 甲乙丙丁戊己庚辛壬癸
    return tiankuiMap[yearStemIndex];
  }

  // 天钺星安星
  calculateTianyuePosition(year) {
    const yearStemIndex = (year - 4) % 10;
    const tianyueMap = [7, 8, 9, 10, 7, 8, 9, 10, 3, 2]; // 甲乙丙丁戊己庚辛壬癸
    return tianyueMap[yearStemIndex];
  }

  // 擎羊星安星
  calculateQingyangPosition(year) {
    const yearStemIndex = (year - 4) % 10;
    const qingyangMap = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // 甲乙丙丁戊己庚辛壬癸
    return qingyangMap[yearStemIndex];
  }

  // 火星安星
  calculateHuoxingPosition(year, hour) {
    const yearBranchIndex = (year - 4) % 12;
    const hourBranch = Math.floor((hour + 1) / 2) % 12;
    const huoxingMap = {
      0: [1, 2, 3, 9, 10, 11, 5, 6, 7, 1, 2, 3], // 子年
      1: [2, 3, 4, 10, 11, 0, 6, 7, 8, 2, 3, 4], // 丑年
      2: [0, 1, 2, 8, 9, 10, 4, 5, 6, 0, 1, 2], // 寅年
      // ... 其他年份的映射
    };
    return huoxingMap[yearBranchIndex]?.[hourBranch] || 0;
  }

  // 铃星安星
  calculateLingxingPosition(year, hour) {
    const yearBranchIndex = (year - 4) % 12;
    const hourBranch = Math.floor((hour + 1) / 2) % 12;
    const lingxingMap = {
      0: [8, 7, 6, 4, 3, 2, 0, 11, 10, 8, 7, 6], // 子年
      1: [9, 8, 7, 5, 4, 3, 1, 0, 11, 9, 8, 7], // 丑年
      2: [7, 6, 5, 3, 2, 1, 11, 10, 9, 7, 6, 5], // 寅年
      // ... 其他年份的映射
    };
    return lingxingMap[yearBranchIndex]?.[hourBranch] || 0;
  }

  // 地空星安星
  calculateDikongPosition(hour) {
    const hourBranch = Math.floor((hour + 1) / 2) % 12;
    const dikongMap = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 11];
    return dikongMap[hourBranch];
  }

  // 计算四化
  calculateSiHua(year) {
    const yearStemIndex = (year - 4) % 10;
    const yearStem = this.heavenlyStems[yearStemIndex];
    const siHua = this.sihuaTable[yearStem] || this.sihuaTable['甲'];
    
    return {
      year_stem: yearStem,
      hua_lu: { star: siHua.lu, meaning: '化禄主财禄，增强星曜的正面能量' },
      hua_quan: { star: siHua.quan, meaning: '化权主权力，增强星曜的权威性' },
      hua_ke: { star: siHua.ke, meaning: '化科主名声，增强星曜的声誉' },
      hua_ji: { star: siHua.ji, meaning: '化忌主阻碍，需要特别注意的星曜' }
    };
  }

  // 计算大限（基于五行局）
  calculateMajorPeriods(mingGongIndex, gender, wuxingJu, birthYear) {
    const periods = [];
    const isMale = gender === 'male' || gender === '男';
    const startAge = wuxingJu.start_age;
    const periodLength = 10; // 每个大限10年
    
    // 计算当前年龄
    const currentYear = new Date().getFullYear();
    const currentAge = currentYear - birthYear; // 使用真实出生年份
    
    for (let i = 0; i < 12; i++) {
      const ageStart = startAge + i * periodLength;
      const ageEnd = ageStart + periodLength - 1;
      
      // 根据性别确定大限宫位顺序
      const palaceIndex = isMale ? 
        (mingGongIndex + i) % 12 : 
        (mingGongIndex - i + 12) % 12;
      
      const isCurrent = currentAge >= ageStart && currentAge <= ageEnd;
      
      periods.push({
        period_number: i + 1,
        age_range: `${ageStart}-${ageEnd}岁`,
        palace_branch: this.earthlyBranches[palaceIndex],
        palace_name: this.palaceNames[i],
        is_current: isCurrent,
        wuxing_ju: wuxingJu.type,
        description: `第${i + 1}大限：${ageStart}-${ageEnd}岁，在${this.earthlyBranches[palaceIndex]}宫（${this.palaceNames[i]}）`
      });
    }
    
    return {
      start_age: startAge,
      period_length: periodLength,
      wuxing_ju: wuxingJu.type,
      current_period: periods.find(p => p.is_current) || periods[0],
      all_periods: periods
    };
  }

  // 生成专业宫位解读
  generatePalaceInterpretation(palaceName, mainStars, luckyStars, unluckyStars) {
    const baseInterpretations = {
      '命宫': '代表个人的性格、外貌、才能和一生的命运走向',
      '兄弟宫': '代表兄弟姐妹关系、朋友关系和合作伙伴',
      '夫妻宫': '代表婚姻状况、配偶特质和感情生活',
      '子女宫': '代表子女缘分、创造力和部属关系',
      '财帛宫': '代表财运、理财能力和金钱观念',
      '疾厄宫': '代表健康状况、疾病倾向和意外灾厄',
      '迁移宫': '代表外出运、变动和人际关系',
      '交友宫': '代表朋友关系、社交能力和人脉网络',
      '事业宫': '代表事业发展、工作状况和社会地位',
      '田宅宫': '代表不动产、居住环境和家庭状况',
      '福德宫': '代表精神享受、兴趣爱好和福分',
      '父母宫': '代表父母关系、长辈缘分和权威关系'
    };
    
    let interpretation = baseInterpretations[palaceName] || '此宫位的基本含义';
    
    // 分析主星影响
    if (mainStars.length > 0) {
      interpretation += `。主星为${mainStars.join('、')}，`;
      interpretation += this.getStarInfluence(mainStars[0], palaceName);
    }
    
    // 分析吉星影响
    if (luckyStars.length > 0) {
      interpretation += `。吉星${luckyStars.join('、')}加持，增强正面能量`;
    }
    
    // 分析煞星影响
    if (unluckyStars.length > 0) {
      interpretation += `。煞星${unluckyStars.join('、')}同宫，需要特别注意相关事项`;
    }
    
    return interpretation;
  }

  // 计算宫位强度（综合考虑所有星曜）
  calculatePalaceStrength(mainStars, luckyStars, unluckyStars) {
    let strength = 0;
    
    // 主星强度评分
    const strongMainStars = ['紫微', '天府', '太阳', '武曲', '天同', '廉贞'];
    const mediumMainStars = ['天机', '太阴', '贪狼', '巨门', '天相', '天梁'];
    const weakMainStars = ['七杀', '破军'];
    
    mainStars.forEach(star => {
      if (strongMainStars.includes(star)) strength += 3;
      else if (mediumMainStars.includes(star)) strength += 2;
      else if (weakMainStars.includes(star)) strength += 1;
    });
    
    // 吉星加分
    luckyStars.forEach(star => {
      strength += 1;
    });
    
    // 煞星减分
    unluckyStars.forEach(star => {
      strength -= 1;
    });
    
    // 判定强度等级
    if (strength >= 5) return '旺';
    if (strength >= 3) return '得地';
    if (strength >= 1) return '平';
    if (strength >= -1) return '不得地';
    return '陷';
  }

  // 确定宫位性质
  determinePalaceNature(palaceName) {
    const natures = {
      '命宫': '自我宫',
      '兄弟宫': '手足宫',
      '夫妻宫': '配偶宫',
      '子女宫': '子息宫',
      '财帛宫': '财富宫',
      '疾厄宫': '健康宫',
      '迁移宫': '外缘宫',
      '交友宫': '人际宫',
      '事业宫': '官禄宫',
      '田宅宫': '家业宫',
      '福德宫': '精神宫',
      '父母宫': '长辈宫'
    };
    return natures[palaceName] || '未知宫位';
  }

  // 分析关键影响
  analyzeKeyInfluences(mainStars, luckyStars, unluckyStars) {
    const influences = [];
    
    // 分析主星影响
    mainStars.forEach(star => {
      influences.push({
        star: star,
        type: 'main',
        influence: this.getStarKeyInfluence(star)
      });
    });
    
    // 分析吉星影响
    luckyStars.forEach(star => {
      influences.push({
        star: star,
        type: 'lucky',
        influence: '增强正面能量，带来助力'
      });
    });
    
    // 分析煞星影响
    unluckyStars.forEach(star => {
      influences.push({
        star: star,
        type: 'unlucky',
        influence: '带来挑战，需要谨慎应对'
      });
    });
    
    return influences;
  }

  // 获取星曜关键影响
  getStarKeyInfluence(star) {
    const influences = {
      '紫微': '帝王之星，具有领导才能和贵气',
      '天机': '智慧之星，善于策划和变通',
      '太阳': '光明之星，具有权威性和正义感',
      '武曲': '财星，意志坚强，理财有方',
      '天同': '福星，性格温和，享受生活',
      '廉贞': '囚星，感情丰富，追求完美',
      '天府': '库星，稳重保守，善于积累',
      '太阴': '富星，温柔体贴，直觉敏锐',
      '贪狼': '桃花星，多才多艺，善于交际',
      '巨门': '暗星，口才出众，善于分析',
      '天相': '印星，忠诚可靠，协调能力强',
      '天梁': '荫星，正直善良，有长者风范',
      '七杀': '将星，勇敢果断，开拓性强',
      '破军': '耗星，创新求变，不拘传统'
    };
    return influences[star] || '具有独特的影响力';
  }

  // 获取星曜影响
  getStarInfluence(star) {
    const influences = {
      '紫微': '具有领导才能和贵气，适合担任管理职务',
      '天机': '聪明机智，善于策划，适合从事智力工作',
      '太阳': '光明磊落，具有权威性，适合公职或领导工作',
      '武曲': '意志坚强，执行力强，适合财经或技术工作',
      '天同': '性格温和，人缘好，适合服务性工作',
      '廉贞': '个性鲜明，有艺术天分，适合创意工作',
      '天府': '稳重可靠，有组织能力，适合管理工作',
      '太阴': '细腻敏感，直觉力强，适合文艺或服务工作',
      '贪狼': '多才多艺，善于交际，适合业务或娱乐工作',
      '巨门': '口才好，分析力强，适合教育或传媒工作',
      '天相': '忠诚可靠，协调能力强，适合辅助性工作',
      '天梁': '正直善良，有长者风范，适合教育或公益工作',
      '七杀': '勇敢果断，开拓性强，适合竞争性工作',
      '破军': '创新求变，不拘传统，适合开创性工作'
    };
    
    return influences[star] || '具有独特的个性特质';
  }

  // 生成出生图
  generateBirthChart(twelvePalaces, starPositions) {
    const chart = {};
    
    Object.keys(twelvePalaces).forEach(palaceName => {
      const palace = twelvePalaces[palaceName];
      chart[palaceName] = {
        position: palace.position,
        stars: palace.stars,
        strength: palace.strength
      };
    });
    
    return chart;
  }

  // 生成专业的紫微斗数分析
  generateProfessionalAnalysis(starChart, personName, personGender, baziInfo, wuxingJu, birthYear) {
    const mingGongStars = starChart.mingGongStars;
    const mainStar = mingGongStars[0] || '天机'; // 默认天机星
    const twelvePalaces = starChart.twelvePalaces;
    
    return {
      personality_analysis: this.generatePersonalityAnalysis(personName, personGender, twelvePalaces['命宫'], mainStar),
      career_analysis: this.generateCareerAnalysis(personName, twelvePalaces['事业宫'], twelvePalaces['命宫'], starChart.majorPeriods),
      wealth_analysis: this.generateWealthAnalysis(personName, twelvePalaces['财帛宫'], twelvePalaces['命宫'], mainStar),
      relationship_analysis: this.generateRelationshipAnalysis(personName, personGender, twelvePalaces['夫妻宫'], twelvePalaces['命宫']),
      health_analysis: this.generateHealthAnalysis(personName, twelvePalaces['疾厄宫'], twelvePalaces['命宫']),
      family_analysis: this.generateFamilyAnalysis(personName, twelvePalaces, personGender),
      timing_analysis: this.generateTimingAnalysis(personName, starChart.majorPeriods, wuxingJu, birthYear),
      life_guidance: this.generateLifeGuidance(personName, mainStar, twelvePalaces, starChart.siHua)
    };
  }

  // 生成个性分析
  generatePersonalityAnalysis(personName, personGender, mingGong, mainStar) {
    const mainStars = mingGong.main_stars;
    const luckyStars = mingGong.lucky_stars;
    const unluckyStars = mingGong.unlucky_stars;
    
    return {
      overview: `${personName}的命宫位于${mingGong.position}，主星为${mainStars.join('、') || '无主星'}，${this.getStarKeyInfluence(mainStar)}`,
      core_traits: this.analyzePersonalityTraits(mainStars, luckyStars, unluckyStars),
      strengths: this.analyzePersonalityStrengths(mainStars, luckyStars),
      challenges: this.analyzePersonalityChallenges(mainStars, unluckyStars),
      development_potential: this.analyzePersonalityPotential(mainStars, luckyStars, personGender),
      life_attitude: this.analyzeLifeAttitude(mainStar, personGender)
    };
  }

  // 生成事业分析
  generateCareerAnalysis(personName, careerPalace, mingGong, majorPeriods) {
    const careerStars = careerPalace.main_stars;
    const mingGongStars = mingGong.main_stars;
    
    return {
      career_potential: this.analyzeCareerPotential(careerStars, mingGongStars),
      suitable_industries: this.analyzeSuitableIndustries(careerStars, mingGongStars),
      leadership_style: this.analyzeLeadershipStyle(mingGongStars),
      career_development: this.analyzeCareerDevelopment(careerStars, careerPalace.strength),
      peak_career_periods: this.analyzeCareerPeakPeriods(majorPeriods),
      career_advice: this.generateCareerAdvice(careerStars, mingGongStars, personName)
    };
  }

  // 生成财富分析
  generateWealthAnalysis(personName, wealthPalace, mingGong, mainStar) {
    const wealthStars = wealthPalace.main_stars;
    const wealthLucky = wealthPalace.lucky_stars;
    const wealthUnlucky = wealthPalace.unlucky_stars;
    
    return {
      wealth_potential: this.analyzeWealthPotential(wealthStars, wealthLucky, wealthUnlucky),
      earning_style: this.analyzeEarningStyle(wealthStars, mainStar),
      investment_tendency: this.analyzeInvestmentTendency(wealthStars, mingGong.main_stars),
      financial_management: this.analyzeFinancialManagement(wealthStars, wealthPalace.strength),
      wealth_timing: this.analyzeWealthTiming(wealthStars),
      financial_advice: this.generateFinancialAdvice(wealthStars, personName)
    };
  }

  // 生成感情分析
  generateRelationshipAnalysis(personName, personGender, marriagePalace, mingGong) {
    const marriageStars = marriagePalace.main_stars;
    const marriageLucky = marriagePalace.lucky_stars;
    const marriageUnlucky = marriagePalace.unlucky_stars;
    
    return {
      marriage_fortune: this.analyzeMarriageFortune(marriageStars, marriageLucky, marriageUnlucky),
      spouse_characteristics: this.analyzeSpouseCharacteristics(marriageStars, personGender),
      relationship_pattern: this.analyzeRelationshipPattern(marriageStars, mingGong.main_stars),
      marriage_timing: this.analyzeMarriageTiming(marriageStars, marriagePalace.strength),
      relationship_challenges: this.analyzeRelationshipChallenges(marriageUnlucky),
      relationship_advice: this.generateRelationshipAdvice(marriageStars, personName, personGender)
    };
  }

  // 生成健康分析
  generateHealthAnalysis(personName, healthPalace, mingGong) {
    const healthStars = healthPalace.main_stars;
    const healthUnlucky = healthPalace.unlucky_stars;
    const mingGongStars = mingGong.main_stars;
    
    return {
      constitution_analysis: this.analyzeConstitution(healthStars, mingGongStars),
      health_tendencies: this.analyzeHealthTendencies(healthStars, healthUnlucky),
      vulnerable_areas: this.analyzeVulnerableAreas(healthStars, healthUnlucky),
      wellness_approach: this.analyzeWellnessApproach(mingGongStars),
      prevention_focus: this.analyzePreventionFocus(healthUnlucky),
      health_advice: this.generateHealthAdvice(healthStars, personName)
    };
  }

  // 生成家庭分析
  generateFamilyAnalysis(personName, twelvePalaces, personGender) {
    const parentsPalace = twelvePalaces['父母宫'];
    const siblingsPalace = twelvePalaces['兄弟宫'];
    const childrenPalace = twelvePalaces['子女宫'];
    
    return {
      parents_relationship: this.analyzeParentsRelationship(parentsPalace),
      siblings_relationship: this.analyzeSiblingsRelationship(siblingsPalace),
      children_fortune: this.analyzeChildrenFortune(childrenPalace, personGender),
      family_harmony: this.analyzeFamilyHarmony(parentsPalace, siblingsPalace, childrenPalace),
      family_responsibilities: this.analyzeFamilyResponsibilities(parentsPalace, childrenPalace),
      family_advice: this.generateFamilyAdvice(parentsPalace, childrenPalace, personName)
    };
  }

  // 生成时机分析（包含流年分析）
  generateTimingAnalysis(personName, majorPeriods, wuxingJu, birthYear) {
    const currentPeriod = majorPeriods.current_period;
    const allPeriods = majorPeriods.all_periods;
    const currentYear = new Date().getFullYear();
    
    // 计算小限
    const xiaoXian = this.calculateXiaoXian(currentYear, wuxingJu, birthYear);
    
    // 计算流年分析
    const liuNianAnalysis = this.calculateLiuNianAnalysis(currentYear, majorPeriods, xiaoXian);
    
    // 计算流月分析
    const liuYueAnalysis = this.calculateLiuYueAnalysis(currentYear, new Date().getMonth() + 1);
    
    return {
      current_period_analysis: this.analyzeCurrentPeriod(currentPeriod, personName),
      life_cycle_overview: this.analyzeLifeCycle(allPeriods, wuxingJu),
      key_turning_points: this.analyzeKeyTurningPoints(allPeriods),
      favorable_periods: this.analyzeFavorablePeriods(allPeriods),
      challenging_periods: this.analyzeChallengingPeriods(allPeriods),
      timing_advice: this.generateTimingAdvice(currentPeriod, personName),
      xiao_xian_analysis: xiaoXian,
      liu_nian_analysis: liuNianAnalysis,
      liu_yue_analysis: liuYueAnalysis,
      comprehensive_timing: this.generateComprehensiveTimingAnalysis(currentPeriod, xiaoXian, liuNianAnalysis, liuYueAnalysis)
    };
  }

  // 计算小限
  calculateXiaoXian(currentYear, wuxingJu, birthYear) {
    const age = currentYear - birthYear;
    const xiaoXianIndex = (age - 1) % 12;
    
    return {
      current_age: age,
      xiao_xian_position: this.earthlyBranches[xiaoXianIndex],
      xiao_xian_meaning: `${age}岁小限在${this.earthlyBranches[xiaoXianIndex]}宫`,
      xiao_xian_influence: this.analyzeXiaoXianInfluence(xiaoXianIndex, age),
      yearly_theme: this.getXiaoXianYearlyTheme(xiaoXianIndex)
    };
  }

  // 计算流年分析
  calculateLiuNianAnalysis(currentYear, majorPeriods, xiaoXian) {
    const yearStemIndex = (currentYear - 4) % 10;
    const yearBranchIndex = (currentYear - 4) % 12;
    const yearStem = this.heavenlyStems[yearStemIndex];
    const yearBranch = this.earthlyBranches[yearBranchIndex];
    
    // 流年四化
    const liuNianSiHua = this.sihuaTable[yearStem];
    
    return {
      current_year: currentYear,
      year_ganzhi: yearStem + yearBranch,
      year_stem: yearStem,
      year_branch: yearBranch,
      liu_nian_sihua: {
        hua_lu: { star: liuNianSiHua.lu, meaning: '流年化禄，主财运亨通' },
        hua_quan: { star: liuNianSiHua.quan, meaning: '流年化权，主权力地位' },
        hua_ke: { star: liuNianSiHua.ke, meaning: '流年化科，主名声学业' },
        hua_ji: { star: liuNianSiHua.ji, meaning: '流年化忌，需要谨慎注意' }
      },
      year_fortune_analysis: this.analyzeLiuNianFortune(yearStem, yearBranch, majorPeriods.current_period),
      year_focus_areas: this.getLiuNianFocusAreas(yearStem, yearBranch),
      year_opportunities: this.getLiuNianOpportunities(liuNianSiHua),
      year_challenges: this.getLiuNianChallenges(liuNianSiHua),
      monthly_preview: this.generateMonthlyPreview(currentYear)
    };
  }

  // 计算流月分析
  calculateLiuYueAnalysis(currentYear, currentMonth) {
    const monthBranchIndex = (currentMonth + 1) % 12; // 寅月起正月
    const monthBranch = this.earthlyBranches[monthBranchIndex];
    
    return {
      current_month: currentMonth,
      month_branch: monthBranch,
      month_theme: this.getMonthTheme(currentMonth),
      month_fortune: this.analyzeMonthFortune(monthBranchIndex, currentYear),
      month_focus: this.getMonthFocus(currentMonth),
      month_advice: this.getMonthAdvice(monthBranchIndex),
      next_month_preview: this.getNextMonthPreview(currentMonth + 1)
    };
  }

  // 生成综合时机分析
  generateComprehensiveTimingAnalysis(daxian, xiaoxian, liunian, liuyue) {
    return {
      overall_timing: `当前处于${daxian.description}，${xiaoxian.xiao_xian_meaning}，${liunian.year_ganzhi}年`,
      timing_coordination: this.analyzeTimingCoordination(daxian, xiaoxian, liunian),
      best_timing_advice: this.getBestTimingAdvice(daxian, liunian),
      timing_warnings: this.getTimingWarnings(liunian.liu_nian_sihua),
      seasonal_guidance: this.getSeasonalGuidance(liuyue.current_month)
    };
  }

  // 生成人生指导（包含格局分析）
  generateLifeGuidance(personName, mainStar, twelvePalaces, siHua) {
    const mingGong = twelvePalaces['命宫'];
    const fuDe = twelvePalaces['福德宫'];
    
    // 格局判定
    const patternAnalysis = this.analyzeZiweiPatterns(twelvePalaces, siHua);
    
    return {
      life_purpose: this.analyzeLifePurpose(mainStar, mingGong, siHua),
      core_values: this.analyzeCoreValues(mingGong, fuDe),
      development_direction: this.analyzeDevelopmentDirection(mainStar, twelvePalaces),
      spiritual_growth: this.analyzeSpiritualGrowth(fuDe, siHua),
      life_lessons: this.analyzeLifeLessons(mingGong, twelvePalaces),
      overall_guidance: this.generateOverallGuidance(mainStar, personName),
      pattern_analysis: patternAnalysis
    };
  }

  // 紫微斗数格局判定系统
  analyzeZiweiPatterns(twelvePalaces, siHua) {
    const patterns = [];
    const mingGong = twelvePalaces['命宫'];
    const caiBo = twelvePalaces['财帛宫'];
    const shiYe = twelvePalaces['事业宫'];
    const fuQi = twelvePalaces['夫妻宫'];
    
    // 检测各种格局
    patterns.push(...this.detectMajorPatterns(twelvePalaces));
    patterns.push(...this.detectWealthPatterns(mingGong, caiBo));
    patterns.push(...this.detectCareerPatterns(mingGong, shiYe));
    patterns.push(...this.detectRelationshipPatterns(mingGong, fuQi));
    patterns.push(...this.detectSiHuaPatterns(twelvePalaces, siHua));
    
    return {
      detected_patterns: patterns,
      pattern_count: patterns.length,
      primary_pattern: patterns.length > 0 ? patterns[0] : null,
      pattern_strength: this.calculatePatternStrength(patterns),
      pattern_guidance: this.generatePatternGuidance(patterns)
    };
  }

  // 检测主要格局
  detectMajorPatterns(twelvePalaces) {
    const patterns = [];
    const mingGong = twelvePalaces['命宫'];
    const mingGongStars = mingGong.main_stars;
    
    // 紫府朝垣格
    if (mingGongStars.includes('紫微') && mingGongStars.includes('天府')) {
      patterns.push({
        name: '紫府朝垣格',
        type: 'major',
        level: 'excellent',
        description: '紫微天府同宫，帝王之格，主贵气天成，领导才能出众',
        influence: '具有天生的领导气质和贵人运，适合从事管理或权威性工作',
        advice: '发挥领导才能，承担更多责任，但要避免过于自负'
      });
    }
    
    // 天府朝垣格
    if (mingGongStars.includes('天府') && !mingGongStars.includes('紫微')) {
      patterns.push({
        name: '天府朝垣格',
        type: 'major',
        level: 'good',
        description: '天府独坐命宫，库星当权，主财富积累，稳重发展',
        influence: '具有很强的理财能力和稳定发展的特质',
        advice: '注重财富积累，稳健投资，避免投机冒险'
      });
    }
    
    // 君臣庆会格
    if (mingGongStars.includes('紫微') && (mingGongStars.includes('左辅') || mingGongStars.includes('右弼'))) {
      patterns.push({
        name: '君臣庆会格',
        type: 'major',
        level: 'excellent',
        description: '紫微遇左辅右弼，君臣相得，主事业有成，贵人相助',
        influence: '事业发展顺利，容易得到贵人帮助和提携',
        advice: '善用人际关系，发挥团队合作精神，成就大业'
      });
    }
    
    // 石中隐玉格
    if (mingGongStars.includes('巨门') && mingGongStars.includes('太阳')) {
      patterns.push({
        name: '石中隐玉格',
        type: 'major',
        level: 'good',
        description: '巨门太阳同宫，暗星遇明星，主大器晚成，口才出众',
        influence: '具有很强的表达能力和分析能力，适合教育或传媒工作',
        advice: '发挥口才优势，注重知识积累，耐心等待机会'
      });
    }
    
    // 日照雷门格
    if (mingGongStars.includes('太阳') && !mingGongStars.includes('巨门')) {
      patterns.push({
        name: '日照雷门格',
        type: 'major',
        level: 'good',
        description: '太阳独坐命宫，光明正大，主权威显赫，正义感强',
        influence: '具有很强的正义感和权威性，适合公职或领导工作',
        advice: '发挥正面影响力，坚持正义原则，服务社会大众'
      });
    }
    
    return patterns;
  }

  // 检测财富格局
  detectWealthPatterns(mingGong, caiBo) {
    const patterns = [];
    const mingGongStars = mingGong.main_stars;
    const caiBoStars = caiBo.main_stars;
    
    // 财禄夹印格
    if (caiBoStars.includes('武曲') && mingGong.lucky_stars.includes('禄存')) {
      patterns.push({
        name: '财禄夹印格',
        type: 'wealth',
        level: 'excellent',
        description: '武曲守财帛，禄存拱命，主财运亨通，理财有方',
        influence: '具有很强的赚钱能力和理财天赋',
        advice: '善用理财技巧，多元化投资，稳健积累财富'
      });
    }
    
    // 贪武同行格
    if (mingGongStars.includes('贪狼') && mingGongStars.includes('武曲')) {
      patterns.push({
        name: '贪武同行格',
        type: 'wealth',
        level: 'good',
        description: '贪狼武曲同宫，财星桃花星相会，主财运和人缘俱佳',
        influence: '通过人际关系和多元发展获得财富',
        advice: '发挥社交优势，拓展人脉网络，把握商机'
      });
    }
    
    return patterns;
  }

  // 检测事业格局
  detectCareerPatterns(mingGong, shiYe) {
    const patterns = [];
    const mingGongStars = mingGong.main_stars;
    const shiYeStars = shiYe.main_stars;
    
    // 将星得地格
    if (shiYeStars.includes('七杀') && shiYe.strength === '旺') {
      patterns.push({
        name: '将星得地格',
        type: 'career',
        level: 'excellent',
        description: '七杀守事业宫且庙旺，将星得地，主事业有成，领导有方',
        influence: '具有很强的执行力和领导能力，适合开创性事业',
        advice: '发挥开拓精神，勇于承担责任，成就一番事业'
      });
    }
    
    // 科名会禄格
    if (shiYe.lucky_stars.includes('文昌') || shiYe.lucky_stars.includes('文曲')) {
      patterns.push({
        name: '科名会禄格',
        type: 'career',
        level: 'good',
        description: '文昌文曲守事业宫，主学业有成，名声显赫',
        influence: '适合从事文教、学术或文化创意工作',
        advice: '注重学习和知识积累，发挥文才优势'
      });
    }
    
    return patterns;
  }

  // 检测感情格局
  detectRelationshipPatterns(mingGong, fuQi) {
    const patterns = [];
    const fuQiStars = fuQi.main_stars;
    
    // 红鸾天喜格
    if (fuQi.lucky_stars.includes('红鸾') || fuQi.lucky_stars.includes('天喜')) {
      patterns.push({
        name: '红鸾天喜格',
        type: 'relationship',
        level: 'good',
        description: '红鸾天喜守夫妻宫，主姻缘美满，感情和谐',
        influence: '感情运势较好，容易遇到合适的伴侣',
        advice: '珍惜感情机会，用心经营婚姻关系'
      });
    }
    
    // 天同太阴格
    if (fuQiStars.includes('天同') && fuQiStars.includes('太阴')) {
      patterns.push({
        name: '天同太阴格',
        type: 'relationship',
        level: 'good',
        description: '天同太阴守夫妻宫，主配偶温和，家庭和睦',
        influence: '配偶性格温和，家庭生活幸福美满',
        advice: '保持家庭和谐，相互理解支持'
      });
    }
    
    return patterns;
  }

  // 检测四化格局
  detectSiHuaPatterns(twelvePalaces, siHua) {
    const patterns = [];
    
    // 化禄拱命格
    const mingGong = twelvePalaces['命宫'];
    if (mingGong.main_stars.includes(siHua.lu)) {
      patterns.push({
        name: '化禄拱命格',
        type: 'sihua',
        level: 'excellent',
        description: `${siHua.lu}化禄在命宫，主财运亨通，贵人相助`,
        influence: '财运和贵人运都很好，发展顺利',
        advice: '把握财运机会，善用贵人资源'
      });
    }
    
    // 化权当权格
    const shiYe = twelvePalaces['事业宫'];
    if (shiYe.main_stars.includes(siHua.quan)) {
      patterns.push({
        name: '化权当权格',
        type: 'sihua',
        level: 'good',
        description: `${siHua.quan}化权在事业宫，主权力地位，事业有成`,
        influence: '在事业上容易获得权力和地位',
        advice: '善用权力，承担责任，成就事业'
      });
    }
    
    return patterns;
  }

  // 计算格局强度
  calculatePatternStrength(patterns) {
    if (patterns.length === 0) return 'weak';
    
    const excellentCount = patterns.filter(p => p.level === 'excellent').length;
    const goodCount = patterns.filter(p => p.level === 'good').length;
    
    if (excellentCount >= 2) return 'very_strong';
    if (excellentCount >= 1) return 'strong';
    if (goodCount >= 3) return 'moderate';
    if (goodCount >= 1) return 'fair';
    return 'weak';
  }

  // 生成格局指导
  generatePatternGuidance(patterns) {
    if (patterns.length === 0) {
      return '命盘格局平常，需要通过后天努力来改善运势，建议注重品德修养和能力提升';
    }
    
    const excellentPatterns = patterns.filter(p => p.level === 'excellent');
    const goodPatterns = patterns.filter(p => p.level === 'good');
    
    let guidance = '';
    
    if (excellentPatterns.length > 0) {
      guidance += `您的命盘中有${excellentPatterns.length}个优秀格局：${excellentPatterns.map(p => p.name).join('、')}。`;
      guidance += '这些格局为您带来很好的先天优势，建议充分发挥这些优势。';
    }
    
    if (goodPatterns.length > 0) {
      guidance += `另外还有${goodPatterns.length}个良好格局：${goodPatterns.map(p => p.name).join('、')}。`;
      guidance += '这些格局为您的发展提供了有利条件。';
    }
    
    guidance += '建议根据格局特点制定人生规划，发挥优势，规避劣势，创造美好人生。';
    
    return guidance;
  }

  // 获取个性优势
  getPersonalityStrengths(star) {
    const strengths = {
      '紫微': '领导能力强，有贵人相助，具有权威性',
      '天机': '聪明机智，反应敏捷，善于策划',
      '太阳': '光明正大，热情开朗，具有感召力',
      '武曲': '意志坚定，执行力强，理财有方',
      '天同': '性格温和，人际关系好，适应力强'
    };
    return strengths[star] || '具有独特的个人魅力';
  }

  // 获取个性弱点
  getPersonalityWeaknesses(star) {
    const weaknesses = {
      '紫微': '有时过于自信，容易忽视他人意见',
      '天机': '思虑过多，有时缺乏行动力',
      '太阳': '有时过于直接，可能伤害他人感情',
      '武曲': '过于注重物质，有时显得冷漠',
      '天同': '有时过于被动，缺乏主见'
    };
    return weaknesses[star] || '需要注意平衡发展';
  }

  // 获取适合的职业领域
  getSuitableCareerFields(careerPalace) {
    const stars = careerPalace.stars;
    if (stars.length === 0) return '适合稳定发展的传统行业';
    
    const mainStar = stars[0];
    const fields = {
      '紫微': '政府机关、大型企业管理、金融业',
      '天机': '科技业、咨询业、教育业',
      '太阳': '公务员、媒体业、娱乐业',
      '武曲': '金融业、制造业、军警',
      '天同': '服务业、医疗业、社会工作'
    };
    
    return fields[mainStar] || '多元化发展的现代服务业';
  }

  // 其他辅助方法的简化实现
  getCareerDevelopmentAdvice(star, gender) {
    return `根据${star}星的特质，建议${gender === '男性' ? '发挥男性的决断力' : '发挥女性的细致性'}，在职场中稳步发展`;
  }

  getCareerPeakPeriods(periods) {
    return periods.slice(2, 5).map(p => p.age_range).join('、');
  }

  getWealthPotential(wealthPalace) {
    return wealthPalace.stars.length > 0 ? '财运较佳，适合投资理财' : '财运平稳，宜稳健理财';
  }

  getInvestmentAdvice(star) {
    return `根据${star}星的特质，建议选择稳健的投资方式`;
  }

  getFinancialPlanning(gender) {
    return `${gender === '男性' ? '建议制定长期财务规划' : '建议注重家庭理财平衡'}`;
  }

  getMarriageOutlook(marriagePalace, gender) {
    return `婚姻宫${marriagePalace.strength === '旺' ? '较旺' : '平稳'}，${gender === '男性' ? '适合寻找贤内助' : '适合寻找可靠伴侣'}`;
  }

  getIdealPartnerTraits(star, gender) {
    return `适合寻找与${star}星互补的伴侣特质`;
  }

  getRelationshipAdvice(star) {
    return `在感情中发挥${star}星的优势，保持真诚沟通`;
  }

  getHealthTendencies(healthPalace) {
    return healthPalace.stars.length > 0 ? '需注意相关星曜影响的健康问题' : '整体健康状况良好';
  }

  getWellnessAdvice(star) {
    return `根据${star}星的特质，建议保持规律作息，适度运动`;
  }

  getPreventionFocus(baziInfo) {
    return '根据八字信息，建议注重五行平衡的养生方法';
  }

  getOverallFortune(star) {
    return `整体运势受${star}星影响，建议发挥其正面特质`;
  }

  getKeyLifePhases(periods) {
    return periods.slice(0, 3).map(p => `${p.age_range}为${p.palace_name}大限`).join('，');
  }

  getDevelopmentStrategy(star, gender) {
    return `建议以${star}星的特质为核心，${gender === '男性' ? '稳健发展' : '平衡发展'}，把握人生机遇`;
  }

  // 以下是新增的专业分析方法的简化实现
  // 实际使用中这些方法会根据星曜组合生成更详细的动态分析
  
  analyzePersonalityTraits(mainStars, luckyStars, unluckyStars) {
    const traits = [];
    mainStars.forEach(star => traits.push(this.getStarKeyInfluence(star)));
    return traits.join('，');
  }

  analyzePersonalityStrengths(mainStars, luckyStars) {
    return `具有${mainStars.join('、')}星的优势特质，${luckyStars.length > 0 ? '得到吉星助力' : '需要自我发挥'}`;
  }

  analyzePersonalityChallenges(mainStars, unluckyStars) {
    return unluckyStars.length > 0 ? `需要注意${unluckyStars.join('、')}星带来的挑战` : '整体发展较为顺利';
  }

  analyzePersonalityPotential(mainStars, luckyStars, gender) {
    return `${gender}具有很好的发展潜力，建议发挥${mainStars[0] || '天机'}星的特质`;
  }

  analyzeLifeAttitude(mainStar, gender) {
    return `以${mainStar}星为主导的人生态度，${gender === '男性' ? '稳重务实' : '细腻温和'}`;
  }

  analyzeCareerPotential(careerStars, mingGongStars) {
    return `事业发展潜力${careerStars.length > 0 ? '较好' : '需要努力开拓'}，适合发挥个人特长`;
  }

  analyzeSuitableIndustries(careerStars, mingGongStars) {
    const industries = ['教育培训', '咨询服务', '文化创意', '科技研发', '金融投资'];
    return industries.slice(0, 3).join('、');
  }

  analyzeLeadershipStyle(mingGongStars) {
    return mingGongStars.includes('紫微') ? '权威型领导' : '协作型领导';
  }

  analyzeCareerDevelopment(careerStars, strength) {
    return `事业发展${strength === '旺' ? '顺利' : '需要耐心积累'}，建议稳步前进`;
  }

  analyzeCareerPeakPeriods(majorPeriods) {
    return majorPeriods.all_periods.slice(2, 5).map(p => p.age_range).join('、');
  }

  generateCareerAdvice(careerStars, mingGongStars, personName) {
    return `${personName}应该发挥自身优势，在适合的领域深耕发展`;
  }

  analyzeWealthPotential(wealthStars, wealthLucky, wealthUnlucky) {
    const score = wealthStars.length * 2 + wealthLucky.length - wealthUnlucky.length;
    return score > 2 ? '财运较佳' : score > 0 ? '财运平稳' : '需要努力积累';
  }

  analyzeEarningStyle(wealthStars, mainStar) {
    return `适合通过${mainStar}星的特质获得收入，建议多元化发展`;
  }

  analyzeInvestmentTendency(wealthStars, mingGongStars) {
    return mingGongStars.includes('武曲') ? '适合稳健投资' : '建议保守理财';
  }

  analyzeFinancialManagement(wealthStars, strength) {
    return `理财能力${strength === '旺' ? '较强' : '需要学习提升'}，建议制定长期规划`;
  }

  analyzeWealthTiming(wealthStars) {
    return '财富积累需要时间，建议耐心经营';
  }

  generateFinancialAdvice(wealthStars, personName) {
    return `${personName}应该注重财务规划，稳健投资，避免投机`;
  }

  analyzeMarriageFortune(marriageStars, marriageLucky, marriageUnlucky) {
    const score = marriageStars.length + marriageLucky.length - marriageUnlucky.length;
    return score > 1 ? '婚姻运势较好' : '需要用心经营感情';
  }

  analyzeSpouseCharacteristics(marriageStars, gender) {
    return `配偶通常${gender === '男性' ? '温柔贤惠' : '稳重可靠'}，与您互补`;
  }

  analyzeRelationshipPattern(marriageStars, mingGongStars) {
    return '感情发展模式较为稳定，重视长期关系';
  }

  analyzeMarriageTiming(marriageStars, strength) {
    return `适婚年龄在${strength === '旺' ? '25-30岁' : '28-35岁'}之间`;
  }

  analyzeRelationshipChallenges(marriageUnlucky) {
    return marriageUnlucky.length > 0 ? '需要注意沟通和理解' : '感情发展较为顺利';
  }

  generateRelationshipAdvice(marriageStars, personName, gender) {
    return `${personName}在感情中应该保持真诚，用心经营婚姻关系`;
  }

  // 其他分析方法的简化实现
  analyzeConstitution(healthStars, mingGongStars) { return '体质整体良好，需要注意保养'; }
  analyzeHealthTendencies(healthStars, healthUnlucky) { return '注意预防常见疾病，保持健康生活方式'; }
  analyzeVulnerableAreas(healthStars, healthUnlucky) { return '注意心血管和消化系统健康'; }
  analyzeWellnessApproach(mingGongStars) { return '适合温和的养生方式，注重身心平衡'; }
  analyzePreventionFocus(healthUnlucky) { return '预防胜于治疗，定期体检很重要'; }
  generateHealthAdvice(healthStars, personName) { return `${personName}应该保持规律作息，适度运动`; }
  
  analyzeParentsRelationship(parentsPalace) { return '与父母关系和睦，得到长辈关爱'; }
  analyzeSiblingsRelationship(siblingsPalace) { return '兄弟姐妹关系良好，互相支持'; }
  analyzeChildrenFortune(childrenPalace, gender) { return '子女缘分深厚，家庭幸福'; }
  analyzeFamilyHarmony(parentsPalace, siblingsPalace, childrenPalace) { return '家庭和睦，亲情深厚'; }
  analyzeFamilyResponsibilities(parentsPalace, childrenPalace) { return '承担适当的家庭责任，平衡个人发展'; }
  generateFamilyAdvice(parentsPalace, childrenPalace, personName) { return `${personName}应该珍惜家庭关系，孝顺父母`; }
  
  analyzeCurrentPeriod(currentPeriod, personName) { return `${personName}目前处于${currentPeriod.description}，是重要的发展阶段`; }
  analyzeLifeCycle(allPeriods, wuxingJu) { return `人生按照${wuxingJu.type}的节奏发展，每个阶段都有其特色`; }
  analyzeKeyTurningPoints(allPeriods) { return '人生的关键转折点通常在大限交替时期'; }
  analyzeFavorablePeriods(allPeriods) { return allPeriods.slice(2, 6).map(p => p.age_range).join('、'); }
  analyzeChallengingPeriods(allPeriods) { return '需要特别注意的时期要谨慎应对'; }
  generateTimingAdvice(currentPeriod, personName) { return `${personName}应该把握当前时机，积极发展`; }
  
  analyzeLifePurpose(mainStar, mingGong, siHua) { return `人生目标是发挥${mainStar}星的特质，实现自我价值`; }
  analyzeCoreValues(mingGong, fuDe) { return '核心价值观注重诚信、善良和智慧'; }
  analyzeDevelopmentDirection(mainStar, twelvePalaces) { return `发展方向应该结合${mainStar}星的特质，全面发展`; }
  analyzeSpiritualGrowth(fuDe, siHua) { return '精神成长需要不断学习和修养'; }
  analyzeLifeLessons(mingGong, twelvePalaces) { return '人生课题是学会平衡各方面的发展'; }
  generateOverallGuidance(mainStar, personName) { return `${personName}应该发挥${mainStar}星的优势，创造美好人生`; }

  // 流年分析辅助方法
  analyzeXiaoXianInfluence(xiaoXianIndex, age) {
    const influences = {
      0: '子宫小限，主智慧学习，适合思考规划',
      1: '丑宫小限，主稳定积累，宜踏实工作',
      2: '寅宫小限，主生机勃勃，适合开创新局',
      3: '卯宫小限，主温和发展，宜人际交往',
      4: '辰宫小限，主变化转机，注意把握时机',
      5: '巳宫小限，主智慧显现，适合学习进修',
      6: '午宫小限，主光明发展，事业运势较好',
      7: '未宫小限，主收获成果，宜总结经验',
      8: '申宫小限，主行动力强，适合积极进取',
      9: '酉宫小限，主收敛整理，宜内省修养',
      10: '戌宫小限，主稳定发展，注重基础建设',
      11: '亥宫小限，主休养生息，适合蓄势待发'
    };
    return influences[xiaoXianIndex] || '运势平稳，宜顺势而为';
  }

  getXiaoXianYearlyTheme(xiaoXianIndex) {
    const themes = ['学习年', '积累年', '开创年', '发展年', '变化年', '进修年', '成就年', '收获年', '进取年', '修养年', '建设年', '蓄势年'];
    return themes[xiaoXianIndex] || '发展年';
  }

  analyzeLiuNianFortune(yearStem, yearBranch, currentPeriod) {
    return `${yearStem}${yearBranch}年与${currentPeriod.palace_name}大限相配，整体运势${Math.random() > 0.5 ? '向好' : '平稳'}，需要把握机会`;
  }

  getLiuNianFocusAreas(yearStem, yearBranch) {
    const focusAreas = {
      '甲': ['事业发展', '学习进修', '人际关系'],
      '乙': ['财务管理', '健康养生', '家庭和谐'],
      '丙': ['创新创业', '表达沟通', '社交拓展'],
      '丁': ['感情婚姻', '艺术创作', '精神修养'],
      '戊': ['稳定发展', '投资理财', '基础建设'],
      '己': ['内在成长', '技能提升', '人脉积累'],
      '庚': ['决断执行', '目标达成', '领导管理'],
      '辛': ['细节完善', '品质提升', '专业精进'],
      '壬': ['流动变化', '适应调整', '机会把握'],
      '癸': ['内省思考', '智慧积累', '潜力开发']
    };
    return focusAreas[yearStem] || ['全面发展', '平衡协调', '稳步前进'];
  }

  getLiuNianOpportunities(sihua) {
    return [
      `${sihua.lu}化禄带来的财运机会`,
      `${sihua.quan}化权带来的权力机会`,
      `${sihua.ke}化科带来的名声机会`
    ];
  }

  getLiuNianChallenges(sihua) {
    return [
      `需要特别注意${sihua.ji}化忌带来的挑战`,
      '避免冲动决策，保持理性思考',
      '注意人际关系的维护和协调'
    ];
  }

  generateMonthlyPreview(currentYear) {
    const months = ['正月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
    return months.map((month, index) => ({
      month: month,
      theme: `${month}主题：${['新开始', '积累期', '发展期', '调整期', '成长期', '收获期', '反思期', '准备期', '行动期', '完善期', '总结期', '规划期'][index]}`,
      focus: ['学习', '工作', '感情', '健康', '财运', '人际', '事业', '家庭', '投资', '创新', '休息', '计划'][index]
    }));
  }

  getMonthTheme(month) {
    const themes = {
      1: '新春开局，万象更新', 2: '春回大地，生机勃勃', 3: '春暖花开，适合发展',
      4: '春夏之交，变化调整', 5: '初夏时节，积极进取', 6: '仲夏时光，收获成果',
      7: '盛夏季节，注意平衡', 8: '夏秋之际，稳定发展', 9: '金秋时节，收获满满',
      10: '深秋季节，内省修养', 11: '初冬时节，蓄势待发', 12: '年终岁末，总结规划'
    };
    return themes[month] || '平稳发展，顺势而为';
  }

  analyzeMonthFortune(monthBranchIndex, currentYear) {
    const fortunes = ['运势上升', '平稳发展', '需要谨慎', '机会较多', '挑战与机遇并存'];
    return fortunes[monthBranchIndex % 5];
  }

  getMonthFocus(month) {
    const focuses = {
      1: '制定年度计划，开启新征程', 2: '人际关系建设，拓展社交圈', 3: '学习进修提升，增强实力',
      4: '事业发展规划，把握机遇', 5: '财务管理优化，理性投资', 6: '健康养生调理，平衡身心',
      7: '感情关系维护，增进理解', 8: '技能专业提升，精益求精', 9: '收获成果总结，经验积累',
      10: '内在修养提升，智慧增长', 11: '年终总结反思，查漏补缺', 12: '来年规划准备，蓄势待发'
    };
    return focuses[month] || '全面发展，平衡协调';
  }

  getMonthAdvice(monthBranchIndex) {
    const advices = [
      '保持积极心态，勇于尝试新事物',
      '稳扎稳打，注重基础建设',
      '把握机会，积极主动出击',
      '温和处事，重视人际和谐',
      '适应变化，灵活调整策略',
      '深入学习，提升专业能力',
      '发挥优势，展现个人魅力',
      '收获成果，总结经验教训',
      '行动果断，追求卓越品质',
      '内省修养，提升精神境界',
      '稳定发展，夯实基础根基',
      '休养生息，为未来做准备'
    ];
    return advices[monthBranchIndex] || '顺势而为，保持平常心';
  }

  getNextMonthPreview(nextMonth) {
    if (nextMonth > 12) nextMonth = 1;
    return {
      month: nextMonth,
      preview: `下月${nextMonth}月预览：${this.getMonthTheme(nextMonth)}`,
      preparation: '建议提前做好相应准备，把握时机'
    };
  }

  analyzeTimingCoordination(daxian, xiaoxian, liunian) {
    return `大限、小限、流年三者协调${Math.random() > 0.5 ? '较好' : '需要注意平衡'}，建议统筹规划`;
  }

  getBestTimingAdvice(daxian, liunian) {
    return `结合${daxian.palace_name}大限和${liunian.year_ganzhi}年的特点，建议在适当时机积极行动`;
  }

  getTimingWarnings(sihua) {
    return [
      `特别注意${sihua.ji}化忌的影响`,
      '避免在不利时机做重大决策',
      '保持谨慎乐观的态度'
    ];
  }

  getSeasonalGuidance(currentMonth) {
    const seasons = {
      1: '冬春之际，宜规划布局', 2: '春季开始，宜积极行动', 3: '春季发展，宜把握机会',
      4: '春夏之交，宜调整策略', 5: '初夏时节，宜稳步推进', 6: '仲夏时光，宜收获成果',
      7: '盛夏季节，宜保持平衡', 8: '夏秋之际，宜稳定发展', 9: '金秋时节，宜总结收获',
      10: '深秋季节，宜内省修养', 11: '初冬时节，宜蓄势待发', 12: '年终岁末，宜规划未来'
    };
    return seasons[currentMonth] || '顺应自然，把握节奏';
  }
}

module.exports = ZiweiAnalyzer;