// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/policy/remote_commands/crd/crd_uma_logger.h"

#include "base/metrics/histogram_functions.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "chrome/browser/ash/policy/remote_commands/crd/crd_remote_command_utils.h"
#include "components/policy/core/common/cloud/enterprise_metrics.h"
#include "device_management_backend.pb.h"

namespace policy {

CrdUmaLogger::CrdUmaLogger(CrdSessionType session_type,
                           UserSessionType user_session_type)
    : session_type_(session_type), user_session_type_(user_session_type) {}

void CrdUmaLogger::LogSessionLaunchResult(
    ExtendedStartCrdSessionResultCode result_code) {
  base::UmaHistogramEnumeration(
      base::StringPrintf(kMetricDeviceRemoteCommandCrdResultTemplate,
                         FormatCrdSessionType(), FormatUserSessionType()),
      result_code);
}

// Created a separate method to have fixed values for UMA logs.
// The returned strings should not be changed, as they are logged as part of the
// UMA keys.
const char* CrdUmaLogger::FormatUserSessionType() const {
  switch (user_session_type_) {
    case UserSessionType::USER_SESSION_TYPE_UNKNOWN:
      return "UnknownUserSession";
    case UserSessionType::AUTO_LAUNCHED_KIOSK_SESSION:
      return "AutoLaunchedKioskSession";
    case UserSessionType::MANUALLY_LAUNCHED_KIOSK_SESSION:
      return "ManuallyLaunchedKioskSession";
    case UserSessionType::AFFILIATED_USER_SESSION:
      return "AffiliatedUserSession";
    case UserSessionType::UNAFFILIATED_USER_SESSION:
      return "UnaffiliatedUserSession";
    case UserSessionType::MANAGED_GUEST_SESSION:
      return "ManagedGuestSession";
    case UserSessionType::GUEST_SESSION:
      return "GuestSession";
    case UserSessionType::NO_SESSION:
      return "NoUserSession";
  }
}

const char* CrdUmaLogger::FormatCrdSessionType() const {
  switch (session_type_) {
    case CrdSessionType::CRD_SESSION_TYPE_UNKNOWN:
      return "Unknown";
    case CrdSessionType::REMOTE_ACCESS_SESSION:
      return "RemoteAccess";
    case CrdSessionType::REMOTE_SUPPORT_SESSION:
      return "RemoteSupport";
  }
}

}  // namespace policy
