/**
 * 大纲质量评估器
 *
 * 解决 issue #10（没有大纲质量评估）
 * 在大纲生成后对其质量做结构化检查，发现问题可触发重新生成或人工审核。
 */

export interface OutlineQualityResult {
  passed: boolean;
  score: number; // 0-100
  warnings: OutlineWarning[];
}

export interface OutlineWarning {
  type: 'duplicate_title' | 'too_many_chapters' | 'too_few_chapters' | 'empty_summary' | 'empty_keypoints' | 'granularity_issue';
  message: string;
  chapterNumber?: number;
  severity: 'low' | 'medium' | 'high';
}

interface OutlineChapter {
  number: number;
  title: string;
  summary?: string;
  keyPoints?: string[];
  estimatedWords?: number;
  sections?: any[];
}

/**
 * 评估大纲质量
 *
 * @param chapters 大纲章节列表
 * @param scaleConfig 规模配置（含 targetChapters, chapterWords 等）
 * @param genLevel 大纲层级
 */
export function evaluateOutlineQuality(
  chapters: OutlineChapter[],
  scaleConfig: { chapters?: number; chapterWords?: number; totalWords?: number },
  genLevel: number = 2
): OutlineQualityResult {
  const warnings: OutlineWarning[] = [];
  let score = 100;

  if (!chapters || chapters.length === 0) {
    return { passed: false, score: 0, warnings: [{ type: 'too_few_chapters', message: '大纲章节数为空', severity: 'high' }] };
  }

  // 1. 检查章节标题重复
  const titleMap = new Map<string, number[]>();
  chapters.forEach((ch) => {
    const normalized = ch.title?.trim().toLowerCase() || '';
    if (normalized) {
      if (!titleMap.has(normalized)) titleMap.set(normalized, []);
      titleMap.get(normalized)!.push(ch.number);
    }
  });

  titleMap.forEach((numbers, title) => {
    if (numbers.length > 1) {
      warnings.push({
        type: 'duplicate_title',
        message: `章节标题重复: "${title}" 出现在第 ${numbers.join('、')} 章`,
        chapterNumber: numbers[0],
        severity: 'high',
      });
      score -= 15 * (numbers.length - 1);
    }
  });

  // 2. 检查章节数范围
  if (scaleConfig.chapters) {
    const minChapters = Math.floor(scaleConfig.chapters * 0.8);
    const maxChapters = Math.ceil(scaleConfig.chapters * 1.2);

    if (chapters.length > maxChapters) {
      warnings.push({
        type: 'too_many_chapters',
        message: `章节数 ${chapters.length} 超出推荐范围（${minChapters}-${maxChapters}）`,
        severity: 'medium',
      });
      score -= 10;
    } else if (chapters.length < minChapters) {
      warnings.push({
        type: 'too_few_chapters',
        message: `章节数 ${chapters.length} 少于推荐范围（${minChapters}-${maxChapters}）`,
        severity: 'medium',
      });
      score -= 10;
    }
  }

  // 3. 检查每章是否有摘要和知识点
  let emptySummaryCount = 0;
  let emptyKeyPointsCount = 0;

  chapters.forEach((ch) => {
    if (!ch.summary || ch.summary.trim().length < 10) {
      emptySummaryCount++;
    }
    if (!ch.keyPoints || ch.keyPoints.length === 0) {
      emptyKeyPointsCount++;
    }
  });

  if (emptySummaryCount > 0) {
    warnings.push({
      type: 'empty_summary',
      message: `${emptySummaryCount}/${chapters.length} 个章节缺少摘要`,
      severity: emptySummaryCount > chapters.length * 0.3 ? 'high' : 'medium',
    });
    score -= emptySummaryCount * 3;
  }

  if (emptyKeyPointsCount > 0) {
    warnings.push({
      type: 'empty_keypoints',
      message: `${emptyKeyPointsCount}/${chapters.length} 个章节缺少知识点`,
      severity: emptyKeyPointsCount > chapters.length * 0.3 ? 'high' : 'medium',
    });
    score -= emptyKeyPointsCount * 3;
  }

  // 4. 章节知识点粒度检查（太少的要报警）
  chapters.forEach((ch) => {
    if (ch.keyPoints && ch.keyPoints.length > 0 && ch.keyPoints.length < 2) {
      warnings.push({
        type: 'granularity_issue',
        message: `第${ch.number}章知识点过少（${ch.keyPoints.length}个），可能粒度太粗`,
        chapterNumber: ch.number,
        severity: 'low',
      });
      score -= 2;
    }
    if (ch.keyPoints && ch.keyPoints.length > 8) {
      warnings.push({
        type: 'granularity_issue',
        message: `第${ch.number}章知识点过多（${ch.keyPoints.length}个），可能粒度太细`,
        chapterNumber: ch.number,
        severity: 'low',
      });
      score -= 2;
    }
  });

  // 5. 对于 genLevel >= 2，检查节的结构
  if (genLevel >= 2) {
    let chaptersWithoutSections = 0;
    chapters.forEach((ch) => {
      if (!ch.sections || ch.sections.length === 0) {
        chaptersWithoutSections++;
      }
    });
    if (chaptersWithoutSections > 0) {
      warnings.push({
        type: 'granularity_issue',
        message: `${chaptersWithoutSections}/${chapters.length} 章缺少节结构`,
        severity: 'low',
      });
      score -= chaptersWithoutSections * 2;
    }
  }

  // 确保分数在 0-100 之间
  score = Math.max(0, Math.min(100, Math.round(score)));

  return {
    passed: score >= 60 && warnings.filter(w => w.severity === 'high').length === 0,
    score,
    warnings,
  };
}
