// ignore_for_file: avoid_redundant_argument_values

import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:kasy_kit/core/config/app_env.dart';
import 'package:kasy_kit/core/states/user_state_notifier.dart';

part 'environnements.freezed.dart';

// URLs for terms of service and privacy policy
const kTermsUrl = '';
const kPrivacyUrl = '';


final environmentProvider = Provider<Environment>(
  (ref) => Environment.fromEnv(),
);

/// The environment of the app.
/// - dev: Development environment
/// - prod: Production environment
/// Feel free to add more environments with your custom properties if needed.
@freezed
sealed class Environment with _$Environment {
  const factory Environment.dev({
    // Name of the environment (dev, prod, ...) just for debug purpose
    required String name,

    /// Url of your backend API / or Supabase URL / or Firebase Functions region
    required String backendUrl,

    /// RevenueCat API key for Android
    /// (only if you want to use in-app purchases with RevenueCat)
    String? revenueCatAndroidApiKey,

    /// RevenueCat API key for iOS
    /// (only if you want to use in-app purchases with RevenueCat)
    String? revenueCatIOSApiKey,

    /// RevenueCat Web Billing API key (rcb_xxx or rcb_sb_xxx)
    /// (only if you want to use subscriptions on web)
    String? revenueCatWebApiKey,

    /// this is used to open the app store page of your app for reviews
    String? appStoreId,

    /// only if you want to use ads
    String? androidInterstitialAdUnitId,

    /// only if you want to use ads
    String? iOSInterstitialAdUnitID,

    /// Environment variable to handle Mixpanel analytics
    /// You can get it from https://mixpanel.com
    String? mixpanelToken,

    /// The default authentication mode of the app (anonymous or authRequired)
    /// See [AuthenticationMode]
    required AuthenticationMode authenticationMode,
  }) = DevEnvironment;

  const factory Environment.prod({
    required String name,

    /// Url of your backend API / or Supabase URL / or Firebase Functions region
    required String backendUrl,

    /// RevenueCat API key for Android
    /// (only if you want to use in-app purchases with RevenueCat)
    String? revenueCatAndroidApiKey,

    /// RevenueCat API key for iOS
    /// (only if you want to use in-app purchases with RevenueCat)
    String? revenueCatIOSApiKey,

    /// RevenueCat Web Billing API key (rcb_xxx or rcb_sb_xxx)
    /// (only if you want to use subscriptions on web)
    String? revenueCatWebApiKey,

    /// only if you want to use ads
    String? androidInterstitialAdUnitId,

    /// only if you want to use ads
    String? iOSInterstitialAdUnitID,

    /// this is used to open the app store page of your app for reviews
    String? appStoreId,

    /// Sentry is an error reporting tool that will help you to track errors in production
    /// You can get it from https://sentry.io
    /// by default sentry will read the SENTRY_DSN environment variable except for web
    /// you can also setup it directly here. Prefer using environment variables
    String? sentryDsn,

    /// Environment variable to handle Mixpanel analytics
    /// You can get it from https://mixpanel.com
    String? mixpanelToken,

    /// The default authentication mode of the app (anonymous or authRequired)
    /// See [AuthenticationMode]
    required AuthenticationMode authenticationMode,
  }) = ProdEnvironment;

  const Environment._();

  factory Environment.fromEnv() {
    final String environmentInput = AppEnv.env;
    switch (environmentInput) {
      case 'dev':
        return Environment.dev(
          name: 'dev',
          backendUrl: AppEnv.backendUrl,
          appStoreId: '',
          revenueCatAndroidApiKey: AppEnv.rcAndroidApiKey,
          revenueCatIOSApiKey: AppEnv.rcIosApiKey,
          revenueCatWebApiKey: AppEnv.rcWebApiKey,
          mixpanelToken: AppEnv.mixpanelToken,
          authenticationMode: AuthenticationMode.anonymous,
        );
      case 'prod':
        return Environment.prod(
          name: 'production',
          backendUrl: AppEnv.backendUrl,
          appStoreId: AppEnv.appStoreId,
          revenueCatAndroidApiKey: AppEnv.rcAndroidApiKey,
          revenueCatIOSApiKey: AppEnv.rcIosApiKey,
          revenueCatWebApiKey: AppEnv.rcWebApiKey,
          sentryDsn: AppEnv.sentryDsn,
          mixpanelToken: AppEnv.mixpanelToken,
          authenticationMode: AuthenticationMode.anonymous,
        );
      default:
        throw Exception('Unknown environment $environmentInput');
    }
  }

  bool get isRevenueCatConfigured => switch (defaultTargetPlatform) {
    TargetPlatform.iOS =>
      revenueCatIOSApiKey != null && revenueCatIOSApiKey!.isNotEmpty,
    TargetPlatform.android =>
      revenueCatAndroidApiKey != null && revenueCatAndroidApiKey!.isNotEmpty,
    _ => kIsWeb &&
        (revenueCatWebApiKey != null && revenueCatWebApiKey!.isNotEmpty),
  };
}

/// This callback is called when the app is launched.
typedef OnEnvCallback = Future<void> Function();
