import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:telemednet/data/models/doctor.dart'; import '../../controllers/doctor_controller.dart'; class DoctorProfileService { static final String doctorProfileCollectionName = dotenv.env['DOCTOR_PROFILE_COLLECTION_NAME']!; static final FirebaseFirestore _db = FirebaseFirestore.instance; static Future saveDoctorProfile(DoctorController controller) async { try { final User? user = FirebaseAuth.instance.currentUser; if (user == null) { print('No user logged in'); return false; } final String uid = user.uid; final Doctor doctorData = controller.model; final Map doctorJson = doctorData.toJson(); doctorJson['createdAt'] = FieldValue.serverTimestamp(); doctorJson['updatedAt'] = FieldValue.serverTimestamp(); doctorJson['uid'] = uid; await _db .collection(doctorProfileCollectionName) .doc(uid) .set(doctorJson); print('Doctor profile saved successfully'); return true; } catch (e) { print('Error saving doctor profile: $e'); return false; } } static Future getDoctorProfile() async { try { final User? user = FirebaseAuth.instance.currentUser; if (user == null) { print('No user logged in'); return null; } final String uid = user.uid; final DocumentSnapshot doc = await _db.collection(doctorProfileCollectionName).doc(uid).get(); if (!doc.exists) { print('No doctor profile found for this user'); return null; } final data = doc.data() as Map; return Doctor( profile: Profile.fromJson(data['profile']), speciality: data['speciality'], yearsOfExperience: data['yearsOfExperience'], licenseNumber: data['licenseNumber'], profileDescription: data['profileDescription'], digitalSignature: data['digitalSignature'], address: Address.fromJson(data['address']), ); } catch (e) { print('Error fetching doctor profile: $e'); return null; } } static Future updateDoctorProfile(DoctorController controller) async { try { final User? user = FirebaseAuth.instance.currentUser; if (user == null) { print('No user logged in'); return false; } final String uid = user.uid; final Doctor doctorData = controller.model; final Map doctorJson = doctorData.toJson(); doctorJson['updatedAt'] = FieldValue.serverTimestamp(); await _db .collection(doctorProfileCollectionName) .doc(uid) .update(doctorJson); print('Doctor profile updated successfully'); return true; } catch (e) { print('Error updating doctor profile: $e'); return false; } } static Future deleteDoctorProfile() async { try { final User? user = FirebaseAuth.instance.currentUser; if (user == null) { print('No user logged in'); return false; } final String uid = user.uid; await _db.collection(doctorProfileCollectionName).doc(uid).delete(); print('Doctor profile deleted successfully'); return true; } catch (e) { print('Error deleting doctor profile: $e'); return false; } } }