import 'package:comwell_key_app/presentation/screens/pregistration/prereg_request_model.dart';
import 'package:comwell_key_app/domain/repositories/profile_settings_repository.dart';
import 'package:comwell_key_app/services/api.dart';
import 'package:comwell_key_app/utils/locator.dart';
import 'package:flutter/material.dart';

class PreregistrationRepository {
  final Api _api = Api();
  final ProfileSettingsRepository profileSettingsRepository =
      locator<ProfileSettingsRepository>();

  PreregistrationRepository();

/*   Future<PaymentConfigurations?> sessionCheckout(
    String hotelCode,
    String bookingId,
    bool usePoints,
  ) async {
    final response = await _api.createAdyenSession(bookingId, usePoints, hotelCode);
    final clientKey = response["clientKey"] as String;
    final sessionResponse = response["sessionResponse"];
    final payedWithPoints = response["isFullyPaidWithPoints"] as bool;
    if (payedWithPoints) {
      return null;
    }
    final id = sessionResponse["id"] as String;
    final sessionData = sessionResponse["sessionData"] as String;
    final availablePaymentMethods =
        await _api.listAvailablePaymentMethods() as Map<String, dynamic>;

    final scheme = _findPaymentMethod(availablePaymentMethods, "scheme");
    final googlePay = _findPaymentMethod(availablePaymentMethods, "googlePay");
    final applePay = _findPaymentMethod(availablePaymentMethods, "applePay");

    // Use the server response amount for consistency across all payment methods
    final serverAmount = Amount(
        value: sessionResponse["amount"]["value"] as int, currency: "DKK");

    final session = await AdyenCheckout.session.create(
        sessionId: id,
        sessionData: sessionData,
        configuration: dropInConfiguration(serverAmount, clientKey, false));

    return PaymentConfigurations(
        sessionCheckout: session,
        googlePayConfiguration: _getGooglePayComponentConfig(clientKey),
        dropInConfiguration:
            dropInConfiguration(serverAmount, clientKey, false),
        cardComponentConfiguration:
            cardComponentConfiguration(serverAmount, clientKey),
        applePayConfiguration:
            _applePayComponentConfiguration(clientKey, serverAmount, false),
        isFullyPaidWithPoints: payedWithPoints,
        availablePaymentMethods: [scheme!, googlePay!, applePay!]);
  }

  Future<PaymentConfigurations?> fetchSessionForAddingCard() async {
    final response = await _api.createAdyenSessionForCards();
    final availablePaymentMethods =
        await _api.listAvailablePaymentMethods() as Map<String, dynamic>;

    final clientKey = response["clientKey"] as String;
    final sessionResponse = response["sessionResponse"];
    final id = sessionResponse["id"] as String;
    final sessionData = sessionResponse["sessionData"] as String;

    final scheme = _findPaymentMethod(availablePaymentMethods, "scheme");
    final googlePay = _findPaymentMethod(availablePaymentMethods, "googlePay");
    final applePay = _findPaymentMethod(availablePaymentMethods, "applePay");

    final session = await AdyenCheckout.session.create(
      sessionId: id,
      sessionData: sessionData,
      configuration: cardComponentConfiguration(
          Amount(value: 0, currency: "DKK"), clientKey),
    );

    return PaymentConfigurations(
        sessionCheckout: session,
        availablePaymentMethods: [scheme!, googlePay!, applePay!],
        googlePayConfiguration: _getGooglePayComponentConfig(clientKey),
        dropInConfiguration: dropInConfiguration(
            Amount(value: 0, currency: "DKK"), clientKey, true),
        cardComponentConfiguration: cardComponentConfiguration(
            Amount(value: 0, currency: "DKK"), clientKey),
        applePayConfiguration: _applePayComponentConfiguration(
            clientKey, Amount(value: 0, currency: "DKK"), true),
        isFullyPaidWithPoints: false);
  } */

  Future<dynamic> createPreregistration(PreregRequestDto request) async {
    try {
      final response = await _api.preArrival(request);
      
      return response.data!;
    } catch (e) {
      debugPrint("error: $e");
      return null;
    }
  }
}
/* 
  CardComponentConfiguration cardComponentConfiguration(
    Amount amount,
    String clientKey,
  ) {
    return CardComponentConfiguration(
      environment: Environment.test,
      amount: amount,
      cardConfiguration: const CardConfiguration(showStorePaymentField: false),
      clientKey: clientKey,
      countryCode: "DK",
    );
  }

  DropInConfiguration dropInConfiguration(
      Amount amount, String clientKey, bool addCard) {
    return DropInConfiguration(
      environment: Environment.test,
      clientKey: clientKey,
      countryCode: "DK",
      skipListWhenSinglePaymentMethod: true,
      googlePayConfiguration: _googlePayConfiguration,
      applePayConfiguration: _getApplePlayConfig(amount, addCard),
      cardConfiguration: const CardConfiguration(showStorePaymentField: false),
      paymentMethodNames: {
        "scheme": "Credit card",
      },
    );
  }

  GooglePayConfiguration get _googlePayConfiguration =>
      const GooglePayConfiguration(
        googlePayEnvironment: GooglePayEnvironment.test,
      );

  GooglePayComponentConfiguration _getGooglePayComponentConfig(
    String clientKey,
  ) {
    return GooglePayComponentConfiguration(
      environment: Environment.test,
      countryCode: "DK",
      clientKey: clientKey,
      googlePayConfiguration: _googlePayConfiguration,
    );
  }

  ApplePayComponentConfiguration _applePayComponentConfiguration(
    String clientKey,
    Amount amount,
    bool addCard,
  ) {
    return ApplePayComponentConfiguration(
      environment: Environment.test,
      countryCode: "DK",
      clientKey: clientKey,
      applePayConfiguration: _getApplePlayConfig(amount, addCard),
    );
  }

  ApplePayConfiguration _getApplePlayConfig(Amount amount, bool addCard) {
    return ApplePayConfiguration(
        merchantId: merchantId,
        merchantName: merchantName,
        allowOnboarding: true,
        allowShippingContactEditing: true,
        applePaySummaryItems: [
          ApplePaySummaryItem(
              label: addCard ? "add_card".tr() : "total".tr(),
              amount: Amount(value: amount.value, currency: amount.currency),
              type: ApplePaySummaryItemType.definite)
        ]);
  }

  static final Iterable<StoredPaymentMethod> mockStoredPaymentData = [
    1,
    2,
    3,
    4
  ].map((i) => StoredPaymentMethod(
      expiryMonth: "12",
      expiryYear: "2035",
      holderName: "holder name",
      id: "id $i",
      lastFour: "$i$i$i$i",
      brand: "visa"));
}

Map<String, dynamic>? _findPaymentMethod(
  Map<String, dynamic> pmResponse,
  String type,
) {
  final list =
      (pmResponse['paymentMethods'] as List?)?.cast<Map<String, dynamic>>() ??
          const <Map<String, dynamic>>[];

  for (final m in list) {
    final t = (m['type'] as String?)?.toLowerCase();
    if (t == type.toLowerCase()) {
      return m; // e.g. 'scheme', 'applepay', 'googlepay'
    }
  }
  return null;
}
*/