// managers/referral_form_manager.dart
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/enums.dart';
import 'package:hmg_patient_app_new/features/hmg_services/models/req_models/create_e_referral_model.dart';
import 'package:hmg_patient_app_new/features/hmg_services/models/resq_models/get_all_cities_resp_model.dart';
import 'package:hmg_patient_app_new/features/hmg_services/models/resq_models/relationship_type_resp_mode.dart';
import 'package:hmg_patient_app_new/features/hmg_services/models/ui_models/e_referral_form_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/hospital_model.dart';


class ReferralFormManager extends ChangeNotifier {
  final ReferralFormData _formData = ReferralFormData();
  final FormValidationErrors _errors = FormValidationErrors();

  ReferralFormData get formData => _formData;
  FormValidationErrors get errors => _errors;


  int _searchCriteria = 0; // 0 = Identification Number, 1 = Referral Number
  String? _searchValue;
  String? _searchPhone;

// Getters
  int get searchCriteria => _searchCriteria;
  String? get searchValue => _searchValue;
  String? get searchPhone => _searchPhone;

  void updateRequesterName(String value) {
    _formData.requesterName = value;
    _clearError('requesterName');
  }

  void updateRequesterPhone(String value) {
    _formData.requesterPhone = value;
    _clearError('requesterPhone');
  }

  void updateCountryEnum(CountryEnum value) {
    _formData.countryEnum = value;
  }
  void updatePatientCountryEnum(CountryEnum value) {
    _formData.patientCountryEnum = value;
  }

  void updateRelationship(GetAllRelationshipTypeResponseModel? value) {
    _formData.relationship = value;
      notifyListeners();
    _clearError('relationship');
  }

  void updateOtherRelationshipName(String value) {
    _formData.otherRelationshipName = value;
    _clearError('otherRelationshipName');
  }

  void updatePatientIdentification(String value) {
    _formData.patientIdentification = value;
    _clearError('patientIdentification');
  }

  void updatePatientName(String value) {
    _formData.patientName = value;
    _clearError('patientName');
  }

  void updatePatientPhone(String? value) {
    _formData.patientPhone = value!;
    _clearError('patientPhone');
  }

  void updatePatientCity(GetAllCitiesResponseModel? value) {
    _formData.patientCity = value;
    _clearError('patientCity');
    notifyListeners();
  }

  void updateBranch(HospitalsModel? value) {
    _formData.branch = value;
    _clearError('branch');
  }

  void updateIsPatientInsured(bool value) {
    _formData.isPatientInsured = value;
    if (!value) {
      _formData.insuredPatientImages.clear();
    }
    _clearError('insuredDocument');
  }

  void addMedicalReport(EReferralAttachment attachment) {
    _formData.medicalReportImages.add(attachment);
    _clearError('medicalReport');
  }

  void removeMedicalReport(int index) {
    if (index >= 0 && index < _formData.medicalReportImages.length) {
      _formData.medicalReportImages.removeAt(index);
    }
    notifyListeners();
  }

  void addInsuranceDocument(EReferralAttachment attachment) {
    _formData.insuredPatientImages.add(attachment);
    _clearError('insuredDocument');
  }

  void removeInsuranceDocument(int index) {
    if (index >= 0 && index < _formData.insuredPatientImages.length) {
      _formData.insuredPatientImages.removeAt(index);
    }

    notifyListeners();
  }


  // Error management
  void setErrors(FormValidationErrors newErrors) {
    _errors.requesterName = newErrors.requesterName;
    _errors.requesterPhone = newErrors.requesterPhone;
    _errors.relationship = newErrors.relationship;
    _errors.otherRelationshipName = newErrors.otherRelationshipName;
    _errors.patientIdentification = newErrors.patientIdentification;
    _errors.patientName = newErrors.patientName;
    _errors.patientPhone = newErrors.patientPhone;
    _errors.patientCity = newErrors.patientCity;
    _errors.medicalReport = newErrors.medicalReport;
    _errors.branch = newErrors.branch;
    _errors.insuredDocument = newErrors.insuredDocument;
    notifyListeners(); // Only notify when errors change
  }

  void setFieldError(String field, String? message) {
    switch (field) {
      case 'medicalReport':
        _errors.medicalReport = message;
        break;
      case 'insuredDocument':
        _errors.insuredDocument = message;
        break;
    }
    notifyListeners();
  }

  void clearAllErrors() {
    _errors.requesterName = null;
    _errors.requesterPhone = null;
    _errors.relationship = null;
    _errors.otherRelationshipName = null;
    _errors.patientIdentification = null;
    _errors.patientName = null;
    _errors.patientPhone = null;
    _errors.patientCity = null;
    _errors.medicalReport = null;
    _errors.branch = null;
    _errors.insuredDocument = null;
    notifyListeners();
  }

  void _clearError(String field) {
    bool shouldNotify = false;

    switch (field) {
      case 'requesterName':
        if (_errors.requesterName != null) {
          _errors.requesterName = null;
          shouldNotify = true;
        }
        break;
      case 'requesterPhone':
        if (_errors.requesterPhone != null) {
          _errors.requesterPhone = null;
          shouldNotify = true;
        }
        break;
      case 'relationship':
        if (_errors.relationship != null) {
          _errors.relationship = null;
          shouldNotify = true;
        }
        break;
      case 'otherRelationshipName':
        if (_errors.otherRelationshipName != null) {
          _errors.otherRelationshipName = null;
          shouldNotify = true;
        }
        break;
      case 'patientIdentification':
        if (_errors.patientIdentification != null) {
          _errors.patientIdentification = null;
          shouldNotify = true;
        }
        break;
      case 'patientName':
        if (_errors.patientName != null) {
          _errors.patientName = null;
          shouldNotify = true;
        }
        break;
      case 'patientPhone':
        if (_errors.patientPhone != null) {
          _errors.patientPhone = null;
          shouldNotify = true;
        }
        break;
      case 'patientCity':
        if (_errors.patientCity != null) {
          _errors.patientCity = null;
          shouldNotify = true;
        }
        break;
      case 'medicalReport':
        if (_errors.medicalReport != null) {
          _errors.medicalReport = null;
          shouldNotify = true;
        }
        break;
      case 'branch':
        if (_errors.branch != null) {
          _errors.branch = null;
          shouldNotify = true;
        }
        break;
      case 'insuredDocument':
        if (_errors.insuredDocument != null) {
          _errors.insuredDocument = null;
          shouldNotify = true;
        }
        break;
    }

    if (shouldNotify) {
      notifyListeners();
    }
  }
  void updateSearchCriteria(int criteria) {
    _searchCriteria = criteria;
    _errors.searchValue = _validateSearchValue();
    notifyListeners();
  }

  void updateSearchValue(String value) {
    _searchValue = value;
    _errors.searchValue = _validateSearchValue();
  }
  void updateSearchPhone(String value) {
    _searchPhone = value;
    _errors.searchPhone = _validateSearchPhone();
  }

  String? _validateSearchValue() {
    if (_searchValue == null || _searchValue!.isEmpty) {
      return _searchCriteria == 0
          ? 'Identification Number is required'
          : 'Referral Number is required';
    }

    if (_searchCriteria == 0) {
      // Validate identification number format
      if (!_isValidIdentificationNumber(_searchValue!)) {
        return 'Please enter a valid identification number';
      }
    } else {
      // Validate referral number format
      if (!_isValidReferralNumber(_searchValue!)) {
        return 'Please enter a valid referral number';
      }
    }

    return null;
  }

  bool _isValidIdentificationNumber(String value) {
    // Add your identification number validation logic
    return value.isNotEmpty; // Basic validation
  }

  bool _isValidReferralNumber(String value) {
    // Add your referral number validation logic
    return value.isNotEmpty; // Basic validation
  }

  void validateSearchForm() {
    _errors.searchValue = _validateSearchValue();
    _errors.searchPhone = _validateSearchPhone();
    notifyListeners();
  }

  String? _validateSearchPhone() {
    if (_searchPhone == null || _searchPhone!.isEmpty) {
      return 'Phone number is required';
    }
    return null;
  }
  bool get isSearchFormInValid {
    return (
        _errors.searchValue != null &&
        _errors.searchValue!.isNotEmpty ||
        _errors.searchPhone != null &&
        _errors.searchPhone!.isNotEmpty);
  }
}