// 振幅数据生成器（7个典型CNC机床阶段）
// 阶段：待机、启动、自检、粗加工、精加工、回零、结束

function randomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function generateStageData(length, base, fluctuation) {
  return Array.from({ length }, () => base + (Math.random() - 0.5) * fluctuation);
}

function generateAmplitudeCycle() {
  const stageNames = [
    '待机', '启动', '自检', '粗加工', '精加工', '回零', '结束'
  ];
  const stageCount = stageNames.length;
  let stages = [];
  for (let i = 0; i < stageCount; i++) {
    stages.push(15); // 每阶段15秒
  }
  const duration = stages.reduce((a, b) => a + b, 0);

  // 各阶段振幅特征，顺序必须和stageNames一致
  const stageParams = [
    { base: 10, fluctuation: 2 },   // 待机
    { base: 18, fluctuation: 4 },   // 启动
    { base: 15, fluctuation: 3 },   // 自检
    { base: 30, fluctuation: 8 },   // 粗加工
    { base: 22, fluctuation: 5 },   // 精加工
    { base: 14, fluctuation: 3 },   // 回零
    { base: 11, fluctuation: 2 }    // 结束
  ];

  let cycle = [];
  for (let i = 0; i < stageCount; i++) {
    const { base, fluctuation } = stageParams[i];
    cycle = cycle.concat(generateStageData(stages[i], base, fluctuation));
  }

  // 随机插入一个异常点
  const abnormalIndex = randomInt(0, cycle.length - 1);
  cycle[abnormalIndex] = cycle[abnormalIndex] + randomInt(20, 40); // 远超正常范围

  return { cycle, stages, duration, stageNames };
}

module.exports = { generateAmplitudeCycle }; 