import type { ArtifactAction, InteractionArtifact } from "./types";

const confirmActions: ArtifactAction[] = [
  {
    actionId: "artifact.confirm",
    label: "确认执行",
    kind: "primary",
    textAliases: ["确认", "确认执行", "执行", "开始", "开始执行", "确定", "没问题"],
    voiceAliases: ["确认执行", "可以执行", "执行吧", "确认修改", "确认写入", "写入吧", "确定执行", "是的"],
    requiresConfirmation: true,
  },
  {
    actionId: "artifact.cancel",
    label: "取消",
    kind: "secondary",
    textAliases: ["取消", "先不执行", "停止"],
    voiceAliases: ["取消执行", "不要执行", "先别执行", "撤销", "返回"],
  },
];

export function createParameterChangeArtifact(input: {
  parameterName: string;
  currentValue: string | number;
  targetValue: string | number;
  unit?: string;
  deviceName?: string;
}): InteractionArtifact {
  const unit = input.unit ?? "";
  return {
    artifactId: `param_${Date.now()}`,
    type: "parameter_change",
    title: `${input.parameterName} 参数修改`,
    status: "waiting_confirmation",
    props: {
      parameterName: input.parameterName,
      currentValue: input.currentValue,
      targetValue: input.targetValue,
      unit,
      deviceName: input.deviceName ?? "一号加工设备",
      impact: "该参数会影响当前工艺节拍，执行前请确认目标值是否正确。",
      executionNote: "Mock 工具将模拟写入参数并返回成功状态。",
    },
    actions: confirmActions,
    llmContext: {
      visibleSummary: `${input.parameterName} 将从 ${input.currentValue}${unit} 修改为 ${input.targetValue}${unit}，当前等待确认。`,
      currentState: "waiting_confirmation",
      visibleFields: [
        { label: "当前值", value: `${input.currentValue}${unit}` },
        { label: "目标值", value: `${input.targetValue}${unit}` },
      ],
      availableActions: confirmActions.map(({ actionId, label, textAliases, voiceAliases }) => ({
        actionId,
        label,
        textAliases,
        voiceAliases,
      })),
      lastEvents: [],
    },
    snapshotPolicy: "session",
  };
}

export function createDeviceActionArtifact(input: {
  deviceName: string;
  action: "open" | "close" | "start" | "stop";
  deviceKind: "light" | "pump" | "fan" | "machine";
}): InteractionArtifact {
  const targetState = input.action === "open" || input.action === "start" ? "运行中" : "已关闭";
  return {
    artifactId: `device_${Date.now()}`,
    type: "device_action",
    title: `${input.deviceName} ${targetState}`,
    status: "waiting_confirmation",
    props: {
      deviceName: input.deviceName,
      action: input.action,
      deviceKind: input.deviceKind,
      currentState: "待机",
      targetState,
      progressLabel: "等待确认",
      executionNote: "Mock 工具将模拟设备动作、动画和状态回读。",
    },
    actions: confirmActions,
    llmContext: {
      visibleSummary: `${input.deviceName} 准备切换到 ${targetState}，当前等待确认。`,
      currentState: "waiting_confirmation",
      visibleFields: [
        { label: "当前状态", value: "待机" },
        { label: "目标状态", value: targetState },
      ],
      availableActions: confirmActions.map(({ actionId, label, textAliases, voiceAliases }) => ({
        actionId,
        label,
        textAliases,
        voiceAliases,
      })),
      lastEvents: [],
    },
    snapshotPolicy: "session",
  };
}

export function createKnowledgeLessonArtifact(topic = "主轴温度报警处理"): InteractionArtifact {
  return {
    artifactId: `knowledge_${Date.now()}`,
    type: "knowledge_lesson",
    title: topic,
    status: "success",
    props: {
      topic,
      conclusion: "先确认冷却、润滑和负载三个方向，再决定是否继续生产。",
      blocks: [
        {
          type: "summary",
          text: "这个报警通常不是单点故障，需要先看冷却流量，再看润滑状态，最后检查最近是否提高了切削负载。",
        },
        {
          type: "key_points",
          items: ["检查冷却泵是否运行", "确认润滑油位和压力", "查看最近 10 分钟主轴负载曲线"],
        },
        {
          type: "steps",
          steps: [
            {
              title: "看冷却",
              body: "确认冷却液液位、过滤器和泵状态，重点看流量是否明显偏低。",
            },
            {
              title: "看润滑",
              body: "确认润滑压力是否在设备手册建议范围内。",
            },
            {
              title: "看负载",
              body: "如果报警前负载持续升高，优先降低进给或暂停加工检查刀具。",
            },
          ],
        },
        {
          type: "details",
          title: "专业详情",
          markdown:
            "主轴温度报警可能来自冷却不足、润滑异常、轴承状态变化或切削负载过高。处理时不要只复位报警，应先确认温升趋势是否已经回落。",
        },
        {
          type: "citations",
          citations: [
            { title: "设备维护手册", location: "第 4 章 主轴系统" },
            { title: "调机知识库", location: "主轴温度报警 SOP v1" },
          ],
        },
      ],
    },
    actions: [
      {
        actionId: "lesson.start_procedure",
        label: "进入调机流程",
        kind: "primary",
        textAliases: ["开始调机", "进入流程", "按步骤处理"],
      },
    ],
    llmContext: {
      visibleSummary: `画布正在展示 ${topic} 的教学卡片。`,
      currentState: "success",
      visibleFields: [{ label: "结论", value: "先确认冷却、润滑和负载三个方向" }],
      availableActions: [
        {
          actionId: "lesson.start_procedure",
          label: "进入调机流程",
          textAliases: ["开始调机", "进入流程", "按步骤处理"],
        },
      ],
      lastEvents: [],
    },
    snapshotPolicy: "ephemeral",
  };
}

export function createGuidedProcedureArtifact(): InteractionArtifact {
  return {
    artifactId: `procedure_${Date.now()}`,
    type: "guided_procedure",
    title: "主轴温度报警调机流程",
    status: "running",
    props: {
      procedureName: "主轴温度报警调机流程",
      deviceName: "一号加工设备",
      currentStepId: "step_1",
      steps: [
        {
          stepId: "step_1",
          type: "auto_tool",
          title: "读取主轴温度和负载",
          instruction: "AI 将模拟读取当前主轴温度、负载和冷却状态。",
          status: "waiting_feedback",
          expectedFeedback: ["已读取", "下一步"],
        },
        {
          stepId: "step_2",
          type: "manual_action",
          title: "检查冷却液流量",
          instruction: "请确认冷却液流量是否稳定，没有明显断流或气泡。",
          status: "pending",
          expectedFeedback: ["已完成", "有异常"],
        },
        {
          stepId: "step_3",
          type: "measurement_input",
          title: "输入润滑压力",
          instruction: "请输入现场读取到的润滑压力，单位 MPa。",
          status: "pending",
          expectedFeedback: ["压力是 0.32", "0.32"],
        },
        {
          stepId: "step_4",
          type: "checklist_gate",
          title: "确认是否恢复生产",
          instruction: "确认温度趋势回落、冷却正常、润滑压力正常后，再恢复生产。",
          status: "pending",
          expectedFeedback: ["确认恢复", "暂停"],
        },
      ],
    },
    actions: [
      {
        actionId: "procedure.next",
        label: "这一步完成",
        kind: "primary",
        textAliases: ["完成", "已完成", "下一步", "这一步完成了"],
      },
      {
        actionId: "procedure.pause",
        label: "暂停流程",
        kind: "secondary",
        textAliases: ["暂停", "先暂停"],
      },
    ],
    llmContext: {
      visibleSummary: "画布正在执行主轴温度报警调机流程，第 1 步等待反馈。",
      currentState: "running",
      activeStep: "读取主轴温度和负载",
      visibleFields: [{ label: "当前步骤", value: "第 1 步：读取主轴温度和负载" }],
      availableActions: [
        {
          actionId: "procedure.next",
          label: "这一步完成",
          textAliases: ["完成", "已完成", "下一步", "这一步完成了"],
        },
        {
          actionId: "procedure.pause",
          label: "暂停流程",
          textAliases: ["暂停", "先暂停"],
        },
      ],
      lastEvents: [],
    },
    snapshotPolicy: "persistent",
  };
}
