import 'package:core/core.dart';
import 'package:feature_inventory/feature_inventory.dart';
import 'package:feature_orders/feature_orders.dart';
import 'package:feature_policy/feature_policy.dart';
import 'package:feature_wordpress/feature_wordpress.dart';

/// Holds the concrete service implementations used by `kell_web`.
///
/// Extends [KcAppServices] from the shared `core` package so that the
/// generic [KcBootstrap] and [KcAppScope] infrastructure can work with
/// this app's specific service set.
///
/// The [AppServices.fake] factory wires up the in-memory fakes that live
/// inside each feature package. The [AppServices.wordpress] factory wires up
/// a real WooCommerce-backed product repository while keeping other services
/// fake until their backends are ready.
class AppServices extends KcAppServices {
  final InventoryRepository inventoryRepository;
  final OrdersRepository ordersRepository;
  final PolicyRepository policyRepository;
  final ProductPublishingRepository productPublishingRepository;

  const AppServices({
    required this.inventoryRepository,
    required this.ordersRepository,
    required this.policyRepository,
    required this.productPublishingRepository,
  });

  /// Creates an [AppServices] backed by fake, in-memory repositories.
  factory AppServices.fake() {
    return AppServices(
      inventoryRepository: FakeInventoryRepository(),
      ordersRepository: FakeOrdersRepository(),
      policyRepository: FakePolicyRepository(),
      productPublishingRepository: FakeProductPublishingRepository(),
    );
  }

  /// Creates an [AppServices] with a real WooCommerce-backed product
  /// repository. Other repositories remain fake until their backends are
  /// ready.
  ///
  /// [siteUrl] – the WordPress site URL (e.g. `https://store.kellcreations.com`).
  /// [consumerKey] – WooCommerce REST API consumer key.
  /// [consumerSecret] – WooCommerce REST API consumer secret.
  factory AppServices.wordpress({
    required String siteUrl,
    required String consumerKey,
    required String consumerSecret,
  }) {
    final apiClient = WooCommerceApiClient(
      siteUrl: siteUrl,
      consumerKey: consumerKey,
      consumerSecret: consumerSecret,
    );

    return AppServices(
      inventoryRepository: FakeInventoryRepository(),
      ordersRepository: FakeOrdersRepository(),
      policyRepository: FakePolicyRepository(),
      productPublishingRepository: WordPressProductPublishingRepository(apiClient: apiClient),
    );
  }

  /// Returns a [KcServiceFactory] for use with [KcBootstrap.run].
  static KcServiceFactory<AppServices> get serviceFactory {
    return KcServiceFactory<AppServices>(
      createFake: () => AppServices.fake(),
      createWordPress: (config) => AppServices.wordpress(
        siteUrl: config.wcSiteUrl,
        consumerKey: config.wcConsumerKey,
        consumerSecret: config.wcConsumerSecret,
      ),
    );
  }
}
