// Copyright 2019 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_PERMISSIONS_PREDICTION_SERVICE_PERMISSION_UI_SELECTOR_H_
#define COMPONENTS_PERMISSIONS_PREDICTION_SERVICE_PERMISSION_UI_SELECTOR_H_

#include <optional>

#include "base/functional/callback_forward.h"
#include "components/permissions/permission_request_enums.h"
#include "components/permissions/request_type.h"

namespace content {
class WebContents;
}

namespace permissions {

class PermissionRequest;
enum PermissionPrediction_Likelihood_DiscretizedLikelihood : int;

// The interface for implementations that decide if the quiet prompt UI should
// be used to display a permission |request|, whether a warning should be
// printed to the Dev Tools console, and the reasons for both.
//
// Implementations of interface are expected to have long-lived instances that
// can support multiple requests, but only one at a time.
class PermissionUiSelector {
 public:
  using PredictionGrantLikelihood =
      PermissionPrediction_Likelihood_DiscretizedLikelihood;

  // LINT.IfChange(QuietUiReason)
  enum class QuietUiReason {
    kEnabledInPrefs,
    kTriggeredByCrowdDeny,
    kTriggeredDueToAbusiveRequests,
    kTriggeredDueToAbusiveContent,
    kServicePredictedVeryUnlikelyGrant,
    kOnDevicePredictedVeryUnlikelyGrant,
    kTriggeredDueToDisruptiveBehavior,
    kTriggeredDueToLackOfGesture,
  };
  // LINT.ThenChange(
  // //chrome/browser/permissions/quiet_permission_prompt_model_android.cc,
  // //chrome/browser/ui/content_settings/content_setting_bubble_model.cc,
  // //components/permissions/permission_request_manager.cc)

  enum class WarningReason {
    kAbusiveRequests,
    kAbusiveContent,
    kDisruptiveBehavior,
  };

  enum class GeolocationAccuracy {
    kUnspecified,
    kPrecise,
    kApproximate,
  };

  struct Decision {
    ~Decision();

    Decision(const Decision&);
    Decision& operator=(const Decision&);

    bool operator==(const Decision&) const;

    static constexpr std::optional<WarningReason> ShowNoWarning() {
      return std::nullopt;
    }

    static Decision UseNormalUiAndShowNoWarning();

    static Decision UseNormalUi(std::optional<WarningReason> warning_reason,
                                GeolocationAccuracy geolocation_accuracy =
                                    GeolocationAccuracy::kUnspecified);

    static Decision UseQuietUi(QuietUiReason quiet_ui_reason,
                               std::optional<WarningReason> warning_reason);

    // The reason for showing the quiet UI, or `std::nullopt` if the normal UI
    // should be used.
    std::optional<QuietUiReason> quiet_ui_reason;

    // The reason for printing a warning to the console, or `std::nullopt` if
    // no warning should be printed.
    std::optional<WarningReason> warning_reason;

    // The preselected geolocation accuracy (for geolocation prompts).
    GeolocationAccuracy geolocation_accuracy;

   private:
    Decision(std::optional<QuietUiReason> quiet_ui_reason,
             std::optional<WarningReason> warning_reason,
             GeolocationAccuracy geolocation_acuracy);
  };

  using DecisionMadeCallback = base::OnceCallback<void(const Decision&)>;

  virtual ~PermissionUiSelector() = default;

  // Determines whether animations should be suppressed because we're very
  // confident the user does not want notifications (e.g. they're abusive).
  static bool ShouldSuppressAnimation(std::optional<QuietUiReason> reason);

  // Determines the UI to use for the given |request|, and invokes |callback|
  // when done, either synchronously or asynchronously. The |callback| is
  // guaranteed never to be invoked after |this| goes out of scope. Only one
  // request is supported at a time.
  virtual void SelectUiToUse(content::WebContents* web_contents,
                             PermissionRequest* request,
                             DecisionMadeCallback callback) = 0;

  // Cancel the pending request, if any. After this, the |callback| is
  // guaranteed not to be invoked anymore, and another call to SelectUiToUse()
  // can be issued. Can be called when there is no pending request which will
  // simply be a no-op.
  virtual void Cancel() {}

  virtual bool IsPermissionRequestSupported(RequestType request_type) = 0;

  // Will return the selector's discretized prediction value, if any is
  // applicable to be recorded in UKMs. This is specific only to a selector that
  // uses of the Web Permission Predictions Service to make decisions.
  virtual std::optional<PredictionGrantLikelihood>
  PredictedGrantLikelihoodForUKM();

  // Will return the selector's discretized permission request relevance, if any
  // is applicable to be recorded in UKMs. This is specific only to a selector
  // that uses Gemini Nano on-device model to make decisions.
  virtual std::optional<PermissionRequestRelevance>
  PermissionRequestRelevanceForUKM();

  // Will return the selector's AI model version, if any is applicable to be
  // recorded in UKMs. This is specific only to a selector that uses an
  // on-device AI model to predict permission relevance score.
  virtual std::optional<permissions::PermissionAiRelevanceModel>
  PermissionAiRelevanceModelForUKM();

  // Will return if the selector's decision was heldback. Currently only the
  // Web Prediction Service selector supports holdbacks.
  virtual std::optional<bool> WasSelectorDecisionHeldback();
};

}  // namespace permissions

#endif  // COMPONENTS_PERMISSIONS_PREDICTION_SERVICE_PERMISSION_UI_SELECTOR_H_
