import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:kasy_kit/core/data/api/firestore_retry.dart';
import 'package:kasy_kit/features/subscription/api/entities/subscription_entity.dart';

final subscriptionApiProvider = Provider(
  (ref) => SubscriptionApi(client: FirebaseFirestore.instance),
);

/// Subscription API
/// Your backend should handle a webhook from the payment provider
/// to update the subscription status
/// Don't save the subscription status in the app,
/// always do this from a webhook call between you backend and the payment provider
class SubscriptionApi {
  final FirebaseFirestore _client;

  SubscriptionApi({
    required FirebaseFirestore client,
  }) : _client = client;

  CollectionReference<SubscriptionEntity?> _getCollection() => _client
      .collection('subscriptions')
      .withConverter(
        fromFirestore: (snapshot, _) {
          if (snapshot.exists) {
            return SubscriptionEntity.fromJson(snapshot.id, snapshot.data()!);
          }
          return null;
        },
        toFirestore: (data, _) => data!.toJson(),
      );

  Future<SubscriptionEntity?> get(String userId) {
    return retryOnFirestoreUnavailable(
      () => _getCollection().doc(userId).get().then((value) => value.data()),
    );
  }
}
