export function parseTimeout(value, fallback, { min, max }) {
  const parsed = Number.parseInt(value || "", 10);
  if (!Number.isInteger(parsed)) return fallback;
  return Math.min(Math.max(parsed, min), max);
}

export function parsePort(value, fallback) {
  const parsed = Number.parseInt(value || "", 10);
  if (!Number.isInteger(parsed) || parsed < 0 || parsed >= 65536) return fallback;
  return parsed;
}

export function applyCloudApiImageEnv(env = process.env) {
  const commitId = env.HWLAB_COMMIT_ID || env.HWLAB_GIT_SHA;
  if (!env.HWLAB_IMAGE && commitId) {
    env.HWLAB_IMAGE = `ghcr.io/pikastech/hwlab-cloud-api:${commitId.slice(0, 7)}`;
  }

  if (!env.HWLAB_IMAGE_TAG && commitId) {
    env.HWLAB_IMAGE_TAG = commitId.slice(0, 7);
  }
}
