const { buildVisibilityDiagnostics } = require("./assistant_visibility");

function evaluateAssistantAvailability({
  user,
  config,
  model,
  runtimeHealth,
  providerAvailable = true,
  origin = "webui",
  platform = origin,
  requestedSurface = "webui_panel",
  roleHint = null
}) {
  const result = buildVisibilityDiagnostics({
    user,
    config,
    model,
    runtimeHealth,
    providerAvailable,
    origin,
    platform,
    requestedSurface,
    roleHint
  });
  const legacyReasons = {
    user_logged_in: "anonymous",
    assistant_enabled: config?.enabled ? "assistant_disabled" : "feature_disabled",
    role_allowed: "role_forbidden",
    model_installed: model ? "model_missing" : "model_not_selected",
    runtime_installed: "runtime_missing",
    runtime_running: `runtime_${runtimeHealth?.state || "stopped"}`,
    runtime_healthy: runtimeHealth?.runtime_usable === false || runtimeHealth?.last_self_test?.success === false
      ? "runtime_unusable"
      : "runtime_unhealthy",
    plugin_enabled: "provider_unavailable"
  };
  return { ...result, reason_code: result.reason_code ? legacyReasons[result.reason_code] || result.reason_code : null };
}

module.exports = { evaluateAssistantAvailability };
