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

#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_METRICS_PAYMENTS_BNPL_METRICS_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_METRICS_PAYMENTS_BNPL_METRICS_H_

#include <string_view>

#include "base/time/time.h"
#include "components/autofill/core/browser/data_model/payments/bnpl_issuer.h"
#include "components/autofill/core/browser/payments/payments_window_manager.h"
#include "services/metrics/public/cpp/ukm_source_id.h"

namespace autofill::autofill_metrics {

using BnplFlowResult = payments::PaymentsWindowManager::BnplFlowResult;

// The reason why a BNPL suggestion was unavailable on the page.
//
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
// LINT.IfChange(BnplSuggestionUnavailableReason)
enum class BnplSuggestionUnavailableReason {
  // The checkout amount could not be extracted from the page. This value is
  // necessary to determine BNPL eligibility for the purchase.
  kAmountExtractionFailure = 0,

  // The checkout amount extracted from the page is not supported by any of the
  // available BNPL issuers.
  kCheckoutAmountNotSupported = 1,

  // Amount extraction timed out while running on the page and the checkout
  // amount was not retrieved. This value is necessary to determine BNPL
  // eligibility for the purchase.
  kAmountExtractionTimeout = 2,

  kMaxValue = kAmountExtractionTimeout,
};
// LINT.ThenChange(/tools/metrics/histograms/metadata/autofill/enums.xml:BnplSuggestionUnavailableReason)

// Enum to track the result of a corresponding BnplTosDialog that was shown.
//
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
//
// LINT.IfChange(BnplTosDialogResult)
enum class BnplTosDialogResult {
  kCancelButtonClicked = 0,
  kAcceptButtonClicked = 1,
  kTabOrBrowserClosed = 2,
  kMaxValue = kTabOrBrowserClosed,
};
// LINT.ThenChange(/tools/metrics/histograms/metadata/autofill/enums.xml:BnplTosDialogResult)

// The dialog close reason of select BNPL issuer dialog.
//
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
//
// LINT.IfChange(SelectBnplIssuerDialogResult)
enum class SelectBnplIssuerDialogResult {
  kCancelButtonClicked = 0,
  kIssuerSelected = 1,
  kTabOrBrowserClosed = 2,
  kMaxValue = kTabOrBrowserClosed,
};
// LINT.ThenChange(/tools/metrics/histograms/metadata/autofill/enums.xml:SelectBnplIssuerDialogResult)

// Returns the histogram suffix corresponding to the given issuer_id.
std::string_view GetHistogramSuffixFromIssuerId(BnplIssuer::IssuerId issuer_id);

// Converts a BnplFlowResult enum to its string representation.
std::string_view ConvertBnplFlowResultToString(BnplFlowResult result);

// LINT.IfChange(BnplFormEvent)

// All BNPL Form Events are logged once per page load.
//
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class BnplFormEvent {
  // Payments autofill suggestions were shown on a BNPL-eligible page,
  // regardless of whether a BNPL suggestion was shown.
  kSuggestionsShownOnBnplEligiblePage = 0,

  // The BNPL suggestion was added to the payments autofill dropdown and shown
  // to the user.
  kBnplSuggestionShown = 1,

  // A BNPL suggestion was accepted on the current page.
  kBnplSuggestionAccepted = 2,

  // A form was filled with an Affirm VCN.
  kFormFilledWithAffirm = 3,

  // A form was filled with a Zip VCN.
  kFormFilledWithZip = 4,

  // A form was filled with an Afterpay VCN.
  kFormFilledWithAfterpay = 5,

  // A form was submitted with an Affirm VCN.
  kFormSubmittedWithAffirm = 6,

  // A form was submitted with a Zip VCN.
  kFormSubmittedWithZip = 7,

  // A form was submitted with an Afterpay VCN.
  kFormSubmittedWithAfterpay = 8,

  // A form was filled with an Klarna VCN.
  kFormFilledWithKlarna = 9,

  // A form was submitted with an Klarna VCN.
  kFormSubmittedWithKlarna = 10,

  kMaxValue = kFormSubmittedWithKlarna,
};

// LINT.ThenChange(/tools/metrics/histograms/metadata/autofill/enums.xml:BnplFormEvent)

// LINT.IfChange(PayLaterTabsFormEvent)

// All Pay Later Tab Form Events are logged once per page load.
//
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class PayLaterTabsFormEvent {
  // Payments autofill suggestions were shown on a BNPL-eligible page,
  // regardless of whether the Pay Later Tab was shown.
  kSuggestionsShown = 0,

  // Payments autofill suggestions were shown in Pay Now / Pay Later tabs.
  kSuggestionsShownWithPayLaterTab = 1,

  // Users switched from the Pay Now tab to the Pay Later tab.
  kSwitchedToPayLaterTab = 2,

  // Users switched from the Pay Later tab to the Pay Now tab.
  kSwitchedToPayNowTab = 3,

  // An Affirm BNPL suggestion was accepted.
  kAffirmAccepted = 4,

  // A Zip BNPL suggestion was accepted.
  kZipAccepted = 5,

  // A Klarna BNPL suggestion was accepted.
  kKlarnaAccepted = 6,

  // An Afterpay BNPL suggestion was accepted.
  kAfterpayAccepted = 7,

  // A form was filled with an Affirm VCN.
  kFormFilledWithAffirm = 8,

  // A form was filled with a Zip VCN.
  kFormFilledWithZip = 9,

  // A form was filled with a Klarna VCN.
  kFormFilledWithKlarna = 10,

  // A form was filled with an Afterpay VCN.
  kFormFilledWithAfterpay = 11,

  // A form was submitted with an Affirm VCN.
  kFormSubmittedWithAffirm = 12,

  // A form was submitted with a Zip VCN.
  kFormSubmittedWithZip = 13,

  // A form was submitted with a Klarna VCN.
  kFormSubmittedWithKlarna = 14,

  // A form was submitted with an Afterpay VCN.
  kFormSubmittedWithAfterpay = 15,

  kMaxValue = kFormSubmittedWithAfterpay,
};

// LINT.ThenChange(/tools/metrics/histograms/metadata/autofill/enums.xml:PayLaterTabsFormEvent)

// Logs if the buy-now-pay-later preference is changed by the user through the
// pay-over-time toggle in the payment methods settings page. Records true when
// the user switches on buy-now-pay-later. Records false when the user switches
// off buy-now-pay-later.
void LogBnplPrefToggled(bool enabled);

// Logs the number of BNPL issuers synced at startup.
void LogBnplIssuersSyncedCountAtStartup(int count);

// Logs that the BNPL ToS dialog was shown.
void LogBnplTosDialogShown(BnplIssuer::IssuerId issuer_id);

// Logs that the BNPL ToS dialog closed reason.
void LogBnplTosDialogResult(BnplTosDialogResult result,
                            BnplIssuer::IssuerId issuer_id);

// Logs the select BNPL issuer dialog result.
void LogSelectBnplIssuerDialogResult(SelectBnplIssuerDialogResult result);

// Logs the selection of BNPL issuer from the select BNPL issuer dialog.
void LogBnplIssuerSelection(BnplIssuer::IssuerId issuer_id);

// Logs that the BNPL suggestion was unavailable and the reason why.
void LogBnplSuggestionUnavailableReason(BnplSuggestionUnavailableReason reason);

// Logs that the BNPL popup window was shown.
void LogBnplPopupWindowShown(BnplIssuer::IssuerId issuer_id);

// Logs the result of the BNPL popup window.
void LogBnplPopupWindowResult(BnplIssuer::IssuerId issuer_id,
                              BnplFlowResult result);

// Logs the duration a user took to go through the BNPL flow inside of the
// pop-up window. Broken down by issuer and result, because each issuer and
// each result should be looked at separately.
void LogBnplPopupWindowLatency(base::TimeDelta duration,
                               BnplIssuer::IssuerId issuer_id,
                               BnplFlowResult result);

// Logs suggestion shown events for the Pay Later tab.
void LogSuggestionShownForPayLaterTab(bool contains_pay_later_tab_suggestions,
                                      ukm::SourceId ukm_source_id);

// Logs that a user has switched to the Pay Later tab.
void LogPayLaterTabSelected(ukm::SourceId ukm_source_id);

// Logs that a pay later tab suggestion was accepted.
void LogPayLaterTabSuggestionAccepted(BnplIssuer::IssuerId issuer_id,
                                      ukm::SourceId ukm_source_id);

// Logs Pay Later Tab form events. Please refer to `PayLaterTabsFormEvent` for
// the possible enumerations that can be logged.
void LogPayLaterTabsFormEvent(PayLaterTabsFormEvent event);

// Logs BNPL form events. Please refer to `BnplFormEvent` for the possible
// enumerations that can be logged.
void LogBnplFormEvent(BnplFormEvent event);

// Logs that the BNPL suggestion was added to the payments autofill dropdown and
// shown to the user. Logs to both UMA and UKM.
void LogBnplSuggestionShown(ukm::SourceId ukm_source_id);

// Logs that a BNPL suggestion was accepted on the current page. Logs to both
// UMA and UKM. `credit_card_suggestions_count` is the number of credit card
// suggestions shown to the user.
void LogBnplSuggestionAccepted(ukm::SourceId ukm_source_id,
                               int credit_card_suggestions_count);

// Logs that a form was filled with the BNPL issuer VCN.
void LogFormFilledWithBnplVcn(BnplIssuer::IssuerId issuer_id);

// Logs that a form was submitted with the BNPL issuer VCN.
void LogFormSubmittedWithBnplVcn(BnplIssuer::IssuerId issuer_id);

// Logs that the BNPL issuer selection dialog was shown.
void LogBnplSelectionDialogShown();

}  // namespace autofill::autofill_metrics

#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_METRICS_PAYMENTS_BNPL_METRICS_H_
