import 'dart:convert';

import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mobile_pos/Services/check%20user%20subscription%20limit/subscription_provider.dart';

import '../../currency.dart';
import '../../model/subscription_model.dart';
import '../../model/subscription_plan_model.dart';

Future<SubscriptionModel?> getFreeSubscriptionPackage() async {
  final snapshot = await FirebaseDatabase.instance
      .ref()
      .child('Admin Panel')
      .child('Subscription Plan')
      .orderByKey()
      .get();

  for (var element in snapshot.children) {
    final subscription = SubscriptionPlanModel.fromJson(
      jsonDecode(jsonEncode(element.value)),
    );

    print(subscription.subscriptionName);

    if (subscription.subscriptionName.toLowerCase().trim() == 'free') {
      return SubscriptionModel(
        dueNumber: subscription.dueNumber,
        duration: subscription.duration,
        partiesNumber: subscription.partiesNumber,
        products: subscription.products,
        purchaseNumber: subscription.purchaseNumber,
        saleNumber: subscription.saleNumber,
        subscriptionDate: DateTime.now().toString(),
        subscriptionName: subscription.subscriptionName,
      );
    }
  }

  return null; // free package না পেলে null রিটার্ন
}

Future<SubscriptionPlanModel?> getSubscriptionPackageByName(
    {required String name, required WidgetRef ref}) async {
  try {
    final data = await ref.watch(subscriptionPlansProvider.future);
    for (SubscriptionPlanModel element in data) {
      print('subscription name: ${element.subscriptionName}');
      if (element.subscriptionName.toLowerCase().trim() == name.toLowerCase().trim()) {
        return element;
      }
    }

    return null; // Package not found
  } catch (e) {
    debugPrint('Error getting subscription package: $e');
    return null;
  }
}

Future<SubscriptionModel?> getCurrentSubscriptionPlan() async {
  try {
    final subscriptionRef =
        FirebaseDatabase.instance.ref().child(constUserId).child('Subscription');

    final snapshot = await subscriptionRef.get();

    if (!snapshot.exists) return null; // No subscription found

    final userSubscription = SubscriptionModel.fromJson(
      jsonDecode(jsonEncode(snapshot.value)),
    );

    return userSubscription; // Return current plan
  } catch (e) {
    debugPrint('Error getting current subscription: $e');
    return null;
  }
}

// Future<SubscriptionModel?> getFreeSubscriptionPackage() async {
//   await FirebaseDatabase.instance.ref().child('Admin Panel').child('Subscription Plan').orderByKey().get().then((value) {
//     for (var element in value.children) {
//       final subscription = SubscriptionPlanModel.fromJson(jsonDecode(jsonEncode(element.value)));
//       print(subscription.subscriptionName);
//       if (subscription.subscriptionName.toLowerCase().trim() == 'free') {
//         return SubscriptionModel(
//           dueNumber: subscription.dueNumber,
//           duration: subscription.duration,
//           partiesNumber: subscription.partiesNumber,
//           products: subscription.products,
//           purchaseNumber: subscription.purchaseNumber,
//           saleNumber: subscription.saleNumber,
//           subscriptionDate: DateTime.now().toString(),
//           subscriptionName: subscription.subscriptionName,
//         );
//       }
//     }
//   });
//   return null;
// }
