const fs = require("fs");

function readJson(filePath) {
  if (!fs.existsSync(filePath)) {
    return null;
  }
  return JSON.parse(fs.readFileSync(filePath, "utf8"));
}

async function checkQueueStatus(config) {
  const status = readJson(config.queueStatusFile);
  if (!status) {
    return {
      ok: true,
      code: "queue_status_missing",
      details: {
        file: config.queueStatusFile,
        note: "Queue status file does not exist yet.",
      },
    };
  }

  if (status.ok === false || Number(status.failed || 0) > 0) {
    return {
      ok: false,
      type: "critical",
      code: "queue_error",
      message: status.message || "Queue system reported an error.",
      details: {
        file: config.queueStatusFile,
        ...status,
      },
    };
  }

  return {
    ok: true,
    code: "queue_ok",
    details: {
      file: config.queueStatusFile,
      ...status,
    },
  };
}

async function checkBrowserHeartbeat(config) {
  const heartbeat = readJson(config.browserHeartbeatFile);
  if (!heartbeat) {
    return {
      ok: true,
      code: "browser_heartbeat_missing",
      details: {
        file: config.browserHeartbeatFile,
        note: "Browser heartbeat file does not exist yet.",
      },
    };
  }

  const timestamp = Date.parse(heartbeat.timestamp || "");
  const ageSeconds = Number.isFinite(timestamp) ? Math.round((Date.now() - timestamp) / 1000) : Infinity;
  const stale = ageSeconds > config.browserHeartbeatMaxAgeSeconds;

  if (heartbeat.ok === false || stale) {
    return {
      ok: false,
      type: "critical",
      code: stale ? "browser_heartbeat_stale" : "browser_crashed",
      message: stale
        ? `Browser heartbeat is stale: ${ageSeconds}s old.`
        : heartbeat.message || "Browser reported a crash.",
      details: {
        file: config.browserHeartbeatFile,
        ageSeconds,
        maxAgeSeconds: config.browserHeartbeatMaxAgeSeconds,
        ...heartbeat,
      },
    };
  }

  return {
    ok: true,
    code: "browser_heartbeat_ok",
    details: {
      file: config.browserHeartbeatFile,
      ageSeconds,
      ...heartbeat,
    },
  };
}

async function checkSessionStatus(config) {
  const session = readJson(config.sessionStatusFile);
  if (!session) {
    return {
      ok: true,
      code: "session_status_missing",
      details: {
        file: config.sessionStatusFile,
        note: "Session status file does not exist yet.",
      },
    };
  }

  if (session.valid === false || session.expired === true) {
    return {
      ok: false,
      type: "sessionExpired",
      code: "session_expired",
      message: session.message || "Smile.one account session expired.",
      details: {
        file: config.sessionStatusFile,
        ...session,
      },
    };
  }

  return {
    ok: true,
    code: "session_ok",
    details: {
      file: config.sessionStatusFile,
      ...session,
    },
  };
}

module.exports = {
  checkQueueStatus,
  checkBrowserHeartbeat,
  checkSessionStatus,
};
