import 'package:get/get.dart';
import 'package:dio/dio.dart';
import '../../../core/network/dio_client.dart';
import '../../../core/utils/app_snackbar.dart';
import '../../../core/utils/logger.dart';

class SubscriptionController extends GetxController {
  var plans = <Map<String, dynamic>>[].obs;
  var currentSubscription = Rxn<Map<String, dynamic>>();
  var myPayments = <Map<String, dynamic>>[].obs;
  var isLoading = true.obs;
  var isCreatingPayment = false.obs;
  var activePaymentRequest = Rxn<Map<String, dynamic>>();

  @override
  void onInit() {
    super.onInit();
    loadAll();
  }

  Future<void> loadAll() async {
    isLoading.value = true;
    await Future.wait([
      loadPlans(),
      loadCurrentSubscription(),
      loadMyPayments(),
    ]);
    isLoading.value = false;
  }

  Future<void> loadPlans() async {
    try {
      final res = await DioClient().client.get('subscriptions/plans');
      if (res.data['success'] == true && res.data['data'] != null) {
        plans.value = List<Map<String, dynamic>>.from(res.data['data']);
      }
    } catch (e) {
      AppLogger.error('Failed to load plans', e);
    }
  }

  Future<void> loadCurrentSubscription() async {
    try {
      final res = await DioClient().client.get('subscriptions/current');
      if (res.data['success'] == true && res.data['data'] != null) {
        currentSubscription.value = Map<String, dynamic>.from(res.data['data']);
      }
    } catch (e) {
      AppLogger.error('Failed to load subscription', e);
    }
  }

  Future<void> loadMyPayments() async {
    try {
      final res = await DioClient().client.get('payments/my-requests');
      if (res.data['success'] == true && res.data['data'] != null) {
        myPayments.value = List<Map<String, dynamic>>.from(res.data['data']);
        // Check for active pending payment
        final pending = myPayments.firstWhereOrNull(
          (p) => p['status'] == 'pending' || p['status'] == 'uploaded',
        );
        activePaymentRequest.value = pending;
      }
    } catch (e) {
      AppLogger.error('Failed to load my payments', e);
    }
  }

  Future<Map<String, dynamic>?> createPaymentRequest(String planId) async {
    try {
      isCreatingPayment.value = true;
      final res = await DioClient().client.post('payments/create', data: {'plan_id': planId});
      if (res.data['success'] == true && res.data['data'] != null) {
        final result = Map<String, dynamic>.from(res.data['data']);
        activePaymentRequest.value = result;
        await loadMyPayments();
        return result;
      }
    } on DioException catch (e) {
      if (e.response?.statusCode == 409) {
        // Already has a pending payment — show existing one
        AppSnackbar.showWarning(
          'طلب قائم',
          'لديك طلب دفع قائم بالفعل. قم بإتمامه أو انتظر المراجعة.',
        );
        // Navigate to the existing pending payment
        if (activePaymentRequest.value != null) {
          Get.toNamed('/payment-receipt', arguments: activePaymentRequest.value);
        }
      } else {
        AppLogger.error('Failed to create payment', e);
        AppSnackbar.showError('خطأ', e.response?.data?['message'] ?? 'فشل إنشاء طلب الدفع');
      }
    } catch (e) {
      AppLogger.error('Failed to create payment', e);
    } finally {
      isCreatingPayment.value = false;
    }
    return null;
  }
}
