import assert from "node:assert/strict";

import {
  createMachineSessionLoadSummary,
  machineSessionLoadLogLines,
} from "../../../runtime/ui/ini-panel/session-load-summary.js";
import {
  createSessionSnapshotSummary,
  sessionSnapshotMetadataLogLines,
  sessionSnapshotSummaryLogLines,
} from "../../../runtime/ui/ini-panel/session-summary.js";

const snapshot = {
  metadata: {
    workflow: "save-session-snapshot",
    snapshotLabel: "ui-machine-session/ui-machine-session.json",
    defaultGcodeOpfsPath: "linuxcnc/gcode/ui-session.ngc",
  },
  payload: {
    files: {
      ini: "linuxcnc/machines/xyzab-tdr/machine.ini",
      parameters: "linuxcnc/machines/xyzab-tdr/linuxcnc.var",
      toolTable: "linuxcnc/machines/xyzab-tdr/tool.tbl",
      gcode: "linuxcnc/gcode/ui-session.ngc",
    },
  },
};

const summary = createSessionSnapshotSummary(snapshot);
assert.deepEqual(summary, {
  snapshotLabel: "ui-machine-session/ui-machine-session.json",
  workflow: "save-session-snapshot",
  defaultGcodeOpfsPath: "linuxcnc/gcode/ui-session.ngc",
  iniOpfsPath: "linuxcnc/machines/xyzab-tdr/machine.ini",
  parameterOpfsPath: "linuxcnc/machines/xyzab-tdr/linuxcnc.var",
  toolTableOpfsPath: "linuxcnc/machines/xyzab-tdr/tool.tbl",
  gcodeOpfsPath: "linuxcnc/gcode/ui-session.ngc",
});

assert.deepEqual(
  createSessionSnapshotSummary(snapshot, "override-session/override.json"),
  {
    ...summary,
    snapshotLabel: "override-session/override.json",
  },
);

assert.deepEqual(
  sessionSnapshotSummaryLogLines(summary),
  [
    "Snapshot: ui-machine-session/ui-machine-session.json",
    "Workflow: save-session-snapshot",
    "Default G-code: linuxcnc/gcode/ui-session.ngc",
  ],
);

assert.deepEqual(
  sessionSnapshotMetadataLogLines(summary),
  [
    "Workflow: save-session-snapshot",
    "Default G-code: linuxcnc/gcode/ui-session.ngc",
  ],
);

assert.deepEqual(
  createSessionSnapshotSummary(null),
  {
    snapshotLabel: null,
    workflow: null,
    defaultGcodeOpfsPath: null,
    iniOpfsPath: null,
    parameterOpfsPath: null,
    toolTableOpfsPath: null,
    gcodeOpfsPath: null,
  },
);

const loadSummary = createMachineSessionLoadSummary({
  ini: {
    opfsPath: "linuxcnc/machines/xyzab-tdr/machine.ini",
    wasmPath: "/work/session-machine.ini",
  },
  parameters: {
    opfsPath: "linuxcnc/machines/xyzab-tdr/linuxcnc.var",
    wasmPath: "/work/session-linuxcnc.var",
    result: "restore_parameters=0",
  },
  toolTable: {
    opfsPath: "linuxcnc/machines/xyzab-tdr/tool.tbl",
    wasmPath: "/work/session-tool.tbl",
    result: "tooldata_load=0",
  },
});
assert.deepEqual(loadSummary, {
  iniOpfsPath: "linuxcnc/machines/xyzab-tdr/machine.ini",
  iniWasmPath: "/work/session-machine.ini",
  parameterOpfsPath: "linuxcnc/machines/xyzab-tdr/linuxcnc.var",
  parameterWasmPath: "/work/session-linuxcnc.var",
  parameterResult: "restore_parameters=0",
  toolTableOpfsPath: "linuxcnc/machines/xyzab-tdr/tool.tbl",
  toolTableWasmPath: "/work/session-tool.tbl",
  toolTableResult: "tooldata_load=0",
});

assert.deepEqual(
  machineSessionLoadLogLines(loadSummary),
  [
    "INI: linuxcnc/machines/xyzab-tdr/machine.ini -> /work/session-machine.ini",
    "Parameter file: linuxcnc/machines/xyzab-tdr/linuxcnc.var -> /work/session-linuxcnc.var",
    "Parameters: restore_parameters=0",
    "Tool table file: linuxcnc/machines/xyzab-tdr/tool.tbl -> /work/session-tool.tbl",
    "Tool table: tooldata_load=0",
  ],
);

assert.deepEqual(
  createMachineSessionLoadSummary(null),
  {
    iniOpfsPath: null,
    iniWasmPath: null,
    parameterOpfsPath: null,
    parameterWasmPath: null,
    parameterResult: "",
    toolTableOpfsPath: null,
    toolTableWasmPath: null,
    toolTableResult: "",
  },
);

console.log("ini_panel_session_summary_node_smoke=ok");
