/// Runtime configuration read from `--dart-define` values.
///
/// This is the shared, platform-agnostic configuration model used by all
/// Kell Creations app targets (`kell_web`, `kell_mobile`, etc.).
///
/// The app reads the following compile-time constants:
///
/// | Key                    | Description                                  | Default  |
/// |------------------------|----------------------------------------------|----------|
/// | `KC_ENV`               | `fake` or `wordpress`                        | `fake`   |
/// | `KC_WC_SITE_URL`       | WordPress site URL                           | (empty)  |
/// | `KC_WC_CONSUMER_KEY`   | WooCommerce REST API consumer key            | (empty)  |
/// | `KC_WC_CONSUMER_SECRET`| WooCommerce REST API consumer secret         | (empty)  |
///
/// Usage:
/// ```sh
/// flutter run -d chrome \
///   --dart-define=KC_ENV=wordpress \
///   --dart-define=KC_WC_SITE_URL=https://store.kellcreations.com \
///   --dart-define=KC_WC_CONSUMER_KEY=ck_xxx \
///   --dart-define=KC_WC_CONSUMER_SECRET=cs_xxx
/// ```
class KcAppConfig {
  /// The environment mode: `fake` or `wordpress`.
  final KcAppEnvironment environment;

  /// WordPress / WooCommerce site URL (only used in [KcAppEnvironment.wordpress]).
  final String wcSiteUrl;

  /// WooCommerce REST API consumer key.
  final String wcConsumerKey;

  /// WooCommerce REST API consumer secret.
  final String wcConsumerSecret;

  const KcAppConfig({
    required this.environment,
    required this.wcSiteUrl,
    required this.wcConsumerKey,
    required this.wcConsumerSecret,
  });

  /// Reads configuration from compile-time `--dart-define` constants.
  factory KcAppConfig.fromEnvironment() {
    const envString = String.fromEnvironment('KC_ENV', defaultValue: 'fake');
    const siteUrl = String.fromEnvironment('KC_WC_SITE_URL');
    const consumerKey = String.fromEnvironment('KC_WC_CONSUMER_KEY');
    const consumerSecret = String.fromEnvironment('KC_WC_CONSUMER_SECRET');

    final environment = KcAppEnvironment.fromString(envString);

    return KcAppConfig(
      environment: environment,
      wcSiteUrl: siteUrl,
      wcConsumerKey: consumerKey,
      wcConsumerSecret: consumerSecret,
    );
  }

  /// Whether the WordPress configuration values are all present and non-empty.
  bool get hasWordPressConfig =>
      wcSiteUrl.isNotEmpty && wcConsumerKey.isNotEmpty && wcConsumerSecret.isNotEmpty;

  /// A human-readable label for the current environment (e.g. for badges).
  String get environmentLabel => environment.label;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is KcAppConfig &&
          environment == other.environment &&
          wcSiteUrl == other.wcSiteUrl &&
          wcConsumerKey == other.wcConsumerKey &&
          wcConsumerSecret == other.wcConsumerSecret;

  @override
  int get hashCode => Object.hash(environment, wcSiteUrl, wcConsumerKey, wcConsumerSecret);

  @override
  String toString() =>
      'KcAppConfig(environment: ${environment.key}, '
      'hasWordPressConfig: $hasWordPressConfig)';
}

/// The supported runtime environments.
enum KcAppEnvironment {
  /// In-memory fakes – no network calls.
  fake('fake', 'FAKE'),

  /// Real WooCommerce backend.
  wordpress('wordpress', 'WP');

  final String key;
  final String label;

  const KcAppEnvironment(this.key, this.label);

  /// Parses a string into a [KcAppEnvironment], defaulting to [fake].
  static KcAppEnvironment fromString(String value) {
    for (final env in values) {
      if (env.key == value.toLowerCase()) return env;
    }
    return fake;
  }
}
