// 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.

#include "chrome/browser/ui/lens/lens_search_feature_flag_utils.h"

#include "base/feature_list.h"
#include "chrome/browser/autocomplete/aim_eligibility_service_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/global_features.h"
#include "components/application_locale_storage/application_locale_storage.h"
#include "components/lens/lens_features.h"
#include "components/lens/lens_overlay_permission_utils.h"
#include "components/omnibox/browser/aim_eligibility_service.h"
#include "components/prefs/pref_service.h"
#include "components/variations/service/variations_service.h"

namespace {
constexpr char kUnitedStatesCountryCode[] = "us";
constexpr char kEnglishUSLocale[] = "en-US";

bool IsEnUs() {
  // Safety check since this is a CP'd change.
  if (!g_browser_process) {
    DCHECK(g_browser_process) << "g_browser_process is null";
    return false;
  }

  // VariationsService and Features should exist.
  auto* variations_service = g_browser_process->variations_service();
  auto* features = g_browser_process->GetFeatures();

  // Safety check since this is a CP'd change.
  if (!variations_service || !features) {
    return false;
  }

  // Otherwise, enable it in the US to en-US locales via client-side code.
  return variations_service->GetStoredPermanentCountry() ==
             kUnitedStatesCountryCode &&
         features->application_locale_storage() &&
         features->application_locale_storage()->Get() == kEnglishUSLocale;
}

}  // namespace

namespace lens {

bool IsLensOverlayContextualSearchboxEnabled(Profile* profile) {
  // If not AIM eligible or fusebox eligible, return false.
  // TODO(crbug.com/545228855): Checking fusebox eligibility is a temporary
  // measure. A new bit should be introduced to the AIM eligibility service for
  // CSB independent of cobrowse and fusebox.
  auto* aim_eligibility_service =
      AimEligibilityServiceFactory::GetForProfile(profile);
  if (!aim_eligibility_service || !aim_eligibility_service->IsAimEligible() ||
      !aim_eligibility_service->IsFuseboxEligible()) {
    return false;
  }

  // If the feature is overridden (e.g. via server-side config or command-line),
  // use that state.
  auto* feature_list = base::FeatureList::GetInstance();
  if (feature_list &&
      (feature_list->IsFeatureOverridden(
          lens::features::kLensOverlayContextualSearchbox.name))) {
    // Important: If a server-side config applies to this client (i.e. after
    // accounting for its filters), but the client gets assigned to the default
    // group, they will still take this code path and receive the state
    // specified via BASE_FEATURE() above.
    return base::FeatureList::IsEnabled(
        lens::features::kLensOverlayContextualSearchbox);
  }

  // Otherwise, return true.
  return true;
}

bool IsAimM3Enabled(Profile* profile) {
  auto* aim_eligibility_service =
      AimEligibilityServiceFactory::GetForProfile(profile);
  // TODO(crbug.com/545228855): Checking fusebox eligibility is a temporary
  // measure. A new bit should be introduced to the AIM eligibility service for
  // CSB independent of cobrowse and fusebox.
  if (!aim_eligibility_service || !aim_eligibility_service->IsAimEligible() ||
      !aim_eligibility_service->IsFuseboxEligible()) {
    return false;
  }

  if (base::FeatureList::IsEnabled(lens::features::kLensSearchAimM3EnUs) &&
      IsEnUs()) {
    return true;
  }

  return base::FeatureList::IsEnabled(lens::features::kLensSearchAimM3);
}

bool DidUserGrantLensOverlayNeededPermissions(Profile* profile) {
  auto* pref_service = profile->GetPrefs();
  if (!CanSharePageScreenshotWithLensOverlay(pref_service)) {
    return false;
  }
  if (IsLensOverlayContextualSearchboxEnabled(profile)) {
    return CanSharePageContentWithLensOverlay(pref_service);
  }
  return true;
}

void GrantLensOverlayNeededPermissions(Profile* profile) {
  auto* pref_service = profile->GetPrefs();
  if (lens::IsLensOverlayContextualSearchboxEnabled(profile)) {
    pref_service->SetBoolean(prefs::kLensSharingPageContentEnabled, true);
  }
  pref_service->SetBoolean(prefs::kLensSharingPageScreenshotEnabled, true);
}

bool MaybeIncrementPrivacyNoticeShownCountAndGrantPermissions(
    Profile* profile) {
  // This function should not be called if the non-blocking privacy notice is
  // not enabled.
  CHECK(lens::features::IsLensOverlayNonBlockingPrivacyNoticeEnabled());

  if (DidUserGrantLensOverlayNeededPermissions(profile)) {
    // User has already granted permissions. Do not show the privacy notice.
    return true;
  }

  int impression_cap =
      features::GetLensOverlayNonBlockingPrivacyNoticeImpressionCap();
  if (impression_cap <= 0) {
    // No impression cap. The privacy notice should be shown.
    return false;
  }

  auto* pref_service = profile->GetPrefs();
  int shown_count = pref_service->GetInteger(
      prefs::kLensOverlayNonBlockingPrivacyNoticeShownCount);
  if (shown_count >= impression_cap) {
    // Shown count has reached impression cap. Grant permissions and do not show
    // the privacy notice.
    GrantLensOverlayNeededPermissions(profile);
    return true;
  }

  // Shown count has not reached impression cap. Increment the count and show
  // the privacy notice.
  pref_service->SetInteger(
      prefs::kLensOverlayNonBlockingPrivacyNoticeShownCount, shown_count + 1);
  return false;
}

}  // namespace lens
