/**
 * Mock 课标覆盖热力图数据（P7-advanced：热力图模块）
 *
 * 维护者：ai13（teacher-portal）
 *
 * 覆盖：
 * - 5 教材 × 50 知识点 × 20 课案覆盖矩阵
 * - 按 textbookId 查询热力图
 */

import type {
  LessonPlanHeatmap,
  HeatmapKnowledgePoint,
  HeatmapLessonPlan,
  HeatmapCell,
} from "@/lib/graphql-p7-advanced";

/** 5 个教材定义 */
const TEXTBOOK_DEFS: Array<{
  textbookId: string;
  textbookTitle: string;
  subject: string;
}> = [
  { textbookId: "tb-001", textbookTitle: "高中数学必修一", subject: "数学" },
  { textbookId: "tb-002", textbookTitle: "高中数学必修二", subject: "数学" },
  { textbookId: "tb-003", textbookTitle: "高中物理必修一", subject: "物理" },
  { textbookId: "tb-004", textbookTitle: "高中化学必修一", subject: "化学" },
  { textbookId: "tb-005", textbookTitle: "高中生物必修一", subject: "生物" },
];

/** 章节名池 */
const CHAPTER_NAMES = [
  "第一章 集合与函数",
  "第二章 基本初等函数",
  "第三章 函数的应用",
  "第四章 空间几何",
  "第五章 点线面位置关系",
];

/** 课案标题池（20 个） */
const LESSON_PLAN_TITLES = [
  "集合的概念",
  "集合间的基本关系",
  "函数及其表示",
  "函数的单调性",
  "函数的奇偶性",
  "指数函数",
  "对数函数",
  "幂函数",
  "函数与方程",
  "函数模型及应用",
  "空间几何体",
  "空间点线面位置关系",
  "直线方程",
  "圆的方程",
  "空间直角坐标系",
  "函数综合复习",
  "三角函数初步",
  "数列初步",
  "不等式",
  "导数初步",
];

/** 知识点名池（50 个，简化生成） */
const KP_NAME_POOL = [
  "集合概念", "集合运算", "并集交集", "补集", "全集",
  "函数定义", "定义域值域", "单调性", "奇偶性", "周期性",
  "指数运算", "指数函数图象", "对数运算", "对数函数图象", "换底公式",
  "幂函数", "幂函数性质", "函数零点", "二分法", "函数模型",
  "几何体结构", "三视图", "直观图", "表面积", "体积",
  "点线面关系", "线线平行", "线面平行", "面面平行", "线线垂直",
  "线面垂直", "面面垂直", "直线倾斜角", "直线斜率", "直线方程形式",
  "圆的定义", "圆的标准方程", "圆的一般方程", "直线与圆位置", "圆与圆位置",
  "空间坐标", "距离公式", "三角函数定义", "正弦定理", "余弦定理",
  "数列概念", "等差数列", "等比数列", "不等式性质", "导数定义",
];

/** 确定性伪随机 */
function seededRandom(seed: number): number {
  const x = Math.sin(seed) * 10000;
  return x - Math.floor(x);
}

function strToSeed(s: string): number {
  let h = 0;
  for (let i = 0; i < s.length; i++) {
    h = (h * 31 + s.charCodeAt(i)) | 0;
  }
  return Math.abs(h) + 1;
}

/** 生成 50 个知识点 */
function generateKnowledgePoints(textbookId: string): HeatmapKnowledgePoint[] {
  return KP_NAME_POOL.map((name, i) => ({
    kpId: `kp-${textbookId}-${String(i + 1).padStart(3, "0")}`,
    name,
    chapterName: CHAPTER_NAMES[i % CHAPTER_NAMES.length] ?? "未分类",
  }));
}

/** 生成 20 个课案 */
function generateLessonPlans(textbookId: string): HeatmapLessonPlan[] {
  return LESSON_PLAN_TITLES.map((title, i) => ({
    planId: `plan-${textbookId}-${String(i + 1).padStart(3, "0")}`,
    title,
  }));
}

/** 生成覆盖矩阵单元格（50 × 20 = 1000 个） */
function generateCells(
  textbookId: string,
  knowledgePoints: HeatmapKnowledgePoint[],
  lessonPlans: HeatmapLessonPlan[],
): HeatmapCell[] {
  const cells: HeatmapCell[] = [];
  for (const kp of knowledgePoints) {
    for (const plan of lessonPlans) {
      const seed = strToSeed(`${kp.kpId}-${plan.planId}`);
      const r = seededRandom(seed);
      // 0-3 覆盖数：约 40% 为 0，25% 为 1，20% 为 2，15% 为 3
      let coverage: number;
      if (r > 0.85) coverage = 3;
      else if (r > 0.65) coverage = 2;
      else if (r > 0.4) coverage = 1;
      else coverage = 0;
      cells.push({ kpId: kp.kpId, planId: plan.planId, coverage });
    }
  }
  return cells;
}

/** 按 textbookId 查询热力图 */
export function findLessonPlanHeatmap(
  textbookId: string,
): LessonPlanHeatmap | null {
  const def = TEXTBOOK_DEFS.find((t) => t.textbookId === textbookId);
  if (!def) return null;
  const knowledgePoints = generateKnowledgePoints(textbookId);
  const lessonPlans = generateLessonPlans(textbookId);
  const cells = generateCells(textbookId, knowledgePoints, lessonPlans);
  return {
    textbookId: def.textbookId,
    textbookTitle: def.textbookTitle,
    knowledgePoints,
    lessonPlans,
    cells,
  };
}

/** 获取所有教材列表（供页面 select 使用） */
export function listHeatmapTextbooks(): Array<{
  textbookId: string;
  textbookTitle: string;
  subject: string;
}> {
  return TEXTBOOK_DEFS;
}
