import assert from "node:assert/strict";

import {
  createMachineSessionReadonlyStatus,
  createMachineSessionReadonlyStatusApiManifest,
  readMachineSessionReadonlyStatus,
} from "../../../runtime/ui/ini-panel/readonly-status-api.js";

const stateBundle = {
  overview: {
    phase: "ran",
    status: {
      lastAction: "run-gcode",
      error: null,
    },
  },
  report: {
    session: {
      snapshotLabel: "ui-machine-session/ui-machine-session.json",
    },
    runReadiness: {
      phase: "ready",
      canRun: true,
    },
    run: {
      status: "ok",
      canonicalEventCount: 2,
    },
  },
};
const workflowStatus = {
  overview: stateBundle.overview,
  sessionReadiness: {
    phase: "ready",
    ready: true,
    missing: [],
  },
  runReadiness: stateBundle.report.runReadiness,
  session: stateBundle.report.session,
  run: stateBundle.report.run,
  lastAction: "run-gcode",
  error: null,
};
const controlView = {
  status: {
    lastAction: "run-gcode",
    runStatus: "ok",
    error: null,
  },
};

assert.deepEqual(
  createMachineSessionReadonlyStatus({
    stateBundle,
    workflowStatus,
    controlView,
  }),
  {
    stateBundle,
    workflowStatus,
    overview: stateBundle.overview,
    controlView,
    report: stateBundle.report,
    sessionReadiness: workflowStatus.sessionReadiness,
    runReadiness: stateBundle.report.runReadiness,
    run: stateBundle.report.run,
    session: stateBundle.report.session,
    status: {
      lastAction: "run-gcode",
      error: null,
      runStatus: "ok",
    },
  },
);

assert.deepEqual(createMachineSessionReadonlyStatus(), {
  stateBundle: null,
  workflowStatus: null,
  overview: null,
  controlView: null,
  report: null,
  sessionReadiness: null,
  runReadiness: null,
  run: null,
  session: null,
  status: {
    lastAction: null,
    error: null,
    runStatus: null,
  },
});

assert.deepEqual(createMachineSessionReadonlyStatusApiManifest(), {
  apiName: "machine-session-readonly-status",
  methods: {
    readStatus: {
      input: "{ getPanelApi, getControlView }",
      output: "readonly machine-session status snapshot",
    },
  },
  relatedControlPageMethods: [
    "loadSessionState",
    "readStatus",
    "refresh",
    "renderView",
  ],
  statusFields: [
    "stateBundle",
    "workflowStatus",
    "overview",
    "controlView",
    "report",
    "sessionReadiness",
    "runReadiness",
    "run",
    "session",
    "status",
  ],
});

const panelApi = {
  getMachineSessionStateBundle: () => stateBundle,
  getMachineSessionWorkflowStatus: () => workflowStatus,
};
assert.deepEqual(
  readMachineSessionReadonlyStatus({
    getPanelApi: () => panelApi,
    getControlView: () => controlView,
  }),
  createMachineSessionReadonlyStatus({
    stateBundle,
    workflowStatus,
    controlView,
  }),
);
assert.deepEqual(
  readMachineSessionReadonlyStatus({
    getPanelApi: () => null,
    getControlView: () => null,
  }),
  createMachineSessionReadonlyStatus(),
);
assert.throws(
  () => readMachineSessionReadonlyStatus({ getControlView: () => null }),
  /panel API getter/,
);
assert.throws(
  () => readMachineSessionReadonlyStatus({ getPanelApi: () => panelApi }),
  /control view getter/,
);

console.log("ini_panel_readonly_status_api_node_smoke=ok");
