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

#import "ios/chrome/browser/credential_provider_promo/ui_bundled/credential_provider_promo_metrics.h"

#import "base/metrics/histogram_functions.h"
#import "base/metrics/user_metrics.h"
#import "base/notreached.h"

const char kIOSCredentialProviderPromoImpressionHistogram[] =
    "IOS.CredentialProviderExtension.Promo.Impression";
const char kIOSCredentialProviderPromoImpressionIsReminderHistogram[] =
    "IOS.CredentialProviderExtension.Promo.Impression.IsReminder";
const char kIOSCredentialProviderPromoOnAutofillUsedHistogram[] =
    "IOS.CredentialProviderExtension.Promo."
    "OnSuccessfulLoginWithAutofilledPassword";
const char kIOSCredentialProviderPromoOnAutofillUsedIsReminderHistogram[] =
    "IOS.CredentialProviderExtension.Promo."
    "OnSuccessfulLoginWithAutofilledPassword.IsReminder";
const char kIOSCredentialProviderPromoOnSetUpListIsReminderHistogram[] =
    "IOS.CredentialProviderExtension.Promo.OnSetUpList.IsReminder";
const char kIOSCredentialProviderPromoOnSetUpListHistogram[] =
    "IOS.CredentialProviderExtension.Promo.OnSetUpList";
const char kIOSCredentialProviderPromoOnTipsNotificationIsReminderHistogram[] =
    "IOS.CredentialProviderExtension.Promo.OnTipsNotification.IsReminder";
const char kIOSCredentialProviderPromoOnTipsNotificationHistogram[] =
    "IOS.CredentialProviderExtension.Promo.OnTipsNotification";
const char kIOSCredentialProviderPromoOnPasskeyCreatedHistogram[] =
    "IOS.CredentialProviderExtension.Promo.OnPasskeyCreated";
const char kIOSCredentialProviderPromoOnPasskeyCreatedIsReminderHistogram[] =
    "IOS.CredentialProviderExtension.Promo."
    "OnPasskeyCreated.IsReminder";

namespace credential_provider_promo {

void RecordImpression(IOSCredentialProviderPromoSource source,
                      bool is_reminder) {
  if (is_reminder) {
    base::UmaHistogramEnumeration(
        kIOSCredentialProviderPromoImpressionIsReminderHistogram, source);
  } else {
    base::UmaHistogramEnumeration(
        kIOSCredentialProviderPromoImpressionHistogram, source);
  }
}

void RecordAction(IOSCredentialProviderPromoSource source,
                  bool is_reminder,
                  IOSCredentialProviderPromoAction action) {
  std::string name;
  switch (source) {
    case IOSCredentialProviderPromoSource::kAutofillUsed:
      name = is_reminder
                 ? kIOSCredentialProviderPromoOnAutofillUsedIsReminderHistogram
                 : kIOSCredentialProviderPromoOnAutofillUsedHistogram;
      break;
    case IOSCredentialProviderPromoSource::kSetUpList:
      name = is_reminder
                 ? kIOSCredentialProviderPromoOnSetUpListIsReminderHistogram
                 : kIOSCredentialProviderPromoOnSetUpListHistogram;
      break;
    case IOSCredentialProviderPromoSource::kTipsNotification:
      name =
          is_reminder
              ? kIOSCredentialProviderPromoOnTipsNotificationIsReminderHistogram
              : kIOSCredentialProviderPromoOnTipsNotificationHistogram;
      break;
    case IOSCredentialProviderPromoSource::kPasskeyCreated:
      name =
          is_reminder
              ? kIOSCredentialProviderPromoOnPasskeyCreatedIsReminderHistogram
              : kIOSCredentialProviderPromoOnPasskeyCreatedHistogram;
      break;
    case IOSCredentialProviderPromoSource::kUnknown:
      NOTREACHED();
  }

  base::UmaHistogramEnumeration(name.data(), action);

  if (action == IOSCredentialProviderPromoAction::kTurnOnAutofill) {
    base::RecordAction(base::UserMetricsAction(
        "MobileCredentialProviderExtensionPromoTurnOnAutoFill"));
  }
}

}  // namespace credential_provider_promo
