import 'package:medora/data/models/patient.dart';
import '../data/services/patient_registration_service.dart';

class PatientController {
  final PatientModel model = PatientModel();
  Map<String, String> validationErrors = {};

  void clearValidationErrors() {
    validationErrors.clear();
  }

  void updateName(String name) {
    model.name = name;
  }

  void updatePhoneNumber(String phoneNumber) {
    model.phoneNumber = phoneNumber;
  }

  void updateGender(String gender) {
    model.gender = gender;
  }

  void updateDateOfBirth(DateTime dateOfBirth) {
    model.dateOfBirth = dateOfBirth;
  }

  void updateProfileImage(String imagePath) {
    model.profileImagePath = imagePath;
  }

  void updateHouseNo(String houseNo) {
    model.address.houseNo = houseNo;
  }

  void updateLine(String line) {
    model.address.line = line;
  }

  void updateTown(String town) {
    model.address.town = town;
  }

  void updatePincode(String pincode) {
    model.address.pincode = pincode;
  }

  void updateCountry(String country) {
    model.address.country = country;
  }

  void updateState(String state) {
    model.address.state = state;
  }

  void updateCity(String city) {
    model.address.city = city;
  }

  void addFamilyMember(FamilyMember member) {
    model.familyMembers.add(member);
  }

  void updateFamilyMember(int index, FamilyMember member) {
    if (index >= 0 && index < model.familyMembers.length) {
      model.familyMembers[index] = member;
    }
  }

  void deleteFamilyMember(int index) {
    if (index >= 0 && index < model.familyMembers.length) {
      model.familyMembers.removeAt(index);
    }
  }

  Future<bool> savePatientData() async {
    return await PatientProfileService.savePatientProfile(this);
  }

  Future<bool> loadPatientData() async {
    PatientModel? loadedModel = await PatientProfileService.getPatientProfile();
    if (loadedModel != null) {
      model.updateFrom(loadedModel);
      return true;
    }
    return false;
  }

  Future<bool> updatePatientData() async {
    return await PatientProfileService.updatePatientProfile(model);
  }

  Future<bool> deletePatientData() async {
    return await PatientProfileService.deletePatientProfile();
  }
}
