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_mobile`.
///
/// Extends [KcAppServices] from the shared `core` package so that the
/// generic [KcBootstrap] and [KcAppScope] infrastructure can work with
/// this app's specific service set.
///
/// Mirrors the same service composition as `kell_web`'s `AppServices`,
/// ensuring both platforms share identical business/domain logic.
class MobileAppServices extends KcAppServices {
  final InventoryRepository inventoryRepository;
  final OrdersRepository ordersRepository;
  final PolicyRepository policyRepository;
  final ProductPublishingRepository productPublishingRepository;

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

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

  /// Creates a [MobileAppServices] with a real WooCommerce-backed product
  /// repository. Other repositories remain fake until their backends are
  /// ready.
  factory MobileAppServices.wordpress({
    required String siteUrl,
    required String consumerKey,
    required String consumerSecret,
  }) {
    final apiClient = WooCommerceApiClient(
      siteUrl: siteUrl,
      consumerKey: consumerKey,
      consumerSecret: consumerSecret,
    );

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

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