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/patient.dart';

import '../../controller/patient_controller.dart';

class PatientProfileService {
  static final String patientProfileCollectionName =
      dotenv.env['PATIENT_PROFILE_COLLECTION_NAME']!;
  static final FirebaseFirestore db = FirebaseFirestore.instance;

  static Future<bool> savePatientProfile(PatientController 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 PatientModel patientData = controller.model;

      final Map<String, dynamic> patientJson = patientData.toJson();
      patientJson['createdAt'] = FieldValue.serverTimestamp();
      patientJson['updatedAt'] = FieldValue.serverTimestamp();
      patientJson['uid'] = uid;

      await db
          .collection(patientProfileCollectionName)
          .doc(uid)
          .set(patientJson);

      print('Patient profile saved successfully');
      return true;
    } catch (e) {
      print('Error saving patient profile: $e');
      return false;
    }
  }

  static Future<PatientModel?> getPatientProfile() 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(patientProfileCollectionName).doc(uid).get();

      if (!doc.exists) {
        print('No patient profile found for this user');
        return null;
      }

      final data = doc.data() as Map<String, dynamic>;
      return PatientModel.fromJson(data);
    } catch (e) {
      print('Error fetching patient profile: $e');
      return null;
    }
  }

  static Future<bool> updatePatientProfile(PatientController 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 PatientModel patientData = controller.model;

      final Map<String, dynamic> patientJson = patientData.toJson();
      patientJson['updatedAt'] = FieldValue.serverTimestamp();

      await db
          .collection(patientProfileCollectionName)
          .doc(uid)
          .update(patientJson);

      print('Patient profile updated successfully');
      return true;
    } catch (e) {
      print('Error updating patient profile: $e');
      return false;
    }
  }

  static Future<bool> deletePatientProfile() 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(patientProfileCollectionName).doc(uid).delete();

      print('Patient profile deleted successfully');
      return true;
    } catch (e) {
      print('Error deleting patient profile: $e');
      return false;
    }
  }
}
