// Copyright 2015 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/affiliations/model/ios_chrome_affiliation_service_factory.h"

#import <memory>
#import <utility>

#import "base/not_fatal_until.h"
#import "base/task/sequenced_task_runner.h"
#import "base/task/thread_pool.h"
#import "components/affiliations/core/browser/affiliation_constants.h"
#import "components/affiliations/core/browser/affiliation_service_impl.h"
#import "ios/chrome/browser/shared/model/application_context/application_context.h"
#import "ios/chrome/browser/shared/model/profile/profile_ios.h"
#import "services/network/public/cpp/shared_url_loader_factory.h"

// static
IOSChromeAffiliationServiceFactory*
IOSChromeAffiliationServiceFactory::GetInstance() {
  static base::NoDestructor<IOSChromeAffiliationServiceFactory> instance;
  return instance.get();
}

// static
affiliations::AffiliationService*
IOSChromeAffiliationServiceFactory::GetForProfile(ProfileIOS* profile) {
  CHECK(profile);

  return GetInstance()
      ->GetServiceForProfileAs<affiliations::AffiliationService>(
          profile, /*create=*/true);
}

IOSChromeAffiliationServiceFactory::IOSChromeAffiliationServiceFactory()
    : ProfileKeyedServiceFactoryIOS("AffiliationService",
                                    ProfileSelection::kRedirectedInIncognito) {}

IOSChromeAffiliationServiceFactory::~IOSChromeAffiliationServiceFactory() =
    default;

std::unique_ptr<KeyedService>
IOSChromeAffiliationServiceFactory::BuildServiceInstanceFor(
    ProfileIOS* profile) const {
  scoped_refptr<base::SequencedTaskRunner> backend_task_runner =
      base::ThreadPool::CreateSequencedTaskRunner(
          {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
           base::TaskShutdownBehavior::BLOCK_SHUTDOWN});
  auto affiliation_service =
      std::make_unique<affiliations::AffiliationServiceImpl>(
          profile->GetSharedURLLoaderFactory(), backend_task_runner);
  affiliation_service->Init(
      GetApplicationContext()->GetNetworkConnectionTracker(),
      profile->GetStatePath().Append(
          affiliations::kAffiliationDatabaseFileName));

  return affiliation_service;
}
