async function run() {
  // No-op: manual mode is event-driven via set.demand → forwardDemand,
  // not tick-driven.
}

async function forwardDemand(ctx, demand) {
  const { machineGroups, machines, logger } = ctx;
  logger?.info?.(`Manual demand forwarded: ${demand}`);

  if (machineGroups && Object.keys(machineGroups).length > 0) {
    await Promise.all(
      Object.values(machineGroups).map((group) =>
        group.handleInput('parent', demand).catch((err) => {
          logger?.error?.(`Failed to forward demand to group: ${err.message}`);
        })
      )
    );
  }

  if (machines && Object.keys(machines).length > 0) {
    const perMachine = demand / Object.keys(machines).length;
    for (const machine of Object.values(machines)) {
      try {
        await machine.handleInput('parent', 'execMovement', perMachine);
      } catch (err) {
        logger?.error?.(`Failed to forward demand to machine: ${err.message}`);
      }
    }
  }
}

module.exports = {
  name: 'manual',
  run,
  forwardDemand,
};
