import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:medora/data/models/telemed_user.dart';

class DataService {
  static final String profileCollectionName =
      dotenv.env['PROFILE_COLLECTION_NAME']!;
  static final db = FirebaseFirestore.instance;

  static User? getCurrentUser() {
    return FirebaseAuth.instance.currentUser;
  }

  static Future<TelemedUser?> getProfile() async {
    try {
      final user = getCurrentUser();
      if (user == null) {
        return null;
      }

      final uid = user.uid;
      final profiles = db.collection(profileCollectionName);
      final profile = await profiles.doc(uid).get();
      if (!profile.exists) {
        return null;
      }
      final profileData = profile.data();
      if (profileData == null) {
        return null;
      }
      var telemedUser = TelemedUser.fromJson(profileData, uid);
      return telemedUser;
    } catch (e) {
      print(e);
      return null;
    }
  }
}
