import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/features/book_appointments/models/resp_models/doctors_list_response_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/doctor_list_api_response.dart' show PatientDoctorAppointmentList;

class DoctorFilterViewModel extends ChangeNotifier{

  late AppState appState;
  DoctorFilterViewModel(){
    appState = getIt();
  }
  List<String> searchedRegionList = [];
  List<String> facilityList = ["hmgHospitals", "hmcMedicalClinic"];
  List<String> searchedHospitalList = [];
  List<PatientDoctorAppointmentList>
  searchedPatientDoctorAppointmentHospitalsList = [];
  List<String> searchedClinicList = [];

  PatientDoctorAppointmentList? selectedHospitalForFilters;
  List<String>? selectedFacilityForFilters = [] , selectedRegionForFilters = [];
  String? selectedClinicForFilters;
  bool applyFilters = false;

  // ==================== FILTER ERROR TRACKING ====================
  // Individual field errors
  String? _hospitalError;
  String? _clinicError;

  // Getters for field errors
  String? get hospitalError => _hospitalError;
  String? get clinicError => _clinicError;

  // Clear hospital error
  void clearHospitalError() {
    _hospitalError = null;
    notifyListeners();
  }

  // Clear clinic error
  void clearClinicError() {
    _clinicError = null;
    notifyListeners();
  }

  // Clear all filter errors
  void clearAllFilterErrors() {
    _hospitalError = null;
    _clinicError = null;
    notifyListeners();
  }

  // Validate filters - shows only first error
  bool validateFilters() {
    final isArabic = appState.isArabic();

    // Clear previous errors
    _hospitalError = null;
    _clinicError = null;

    // Check if at least one hospital is selected
    if (selectedHospitalForFilters == null) {
      _hospitalError = isArabic ? "الرجاء اختيار المستشفى" : "Please select a hospital";
      notifyListeners();
      return false;
    }

    // Check if at least one clinic is selected
    if (selectedClinicForFilters == null || selectedClinicForFilters!.isEmpty) {
      _clinicError = isArabic ? "الرجاء اختيار العيادة" : "Please select a clinic";
      notifyListeners();
      return false;
    }

    // All validations passed
    notifyListeners();
    return true;
  }

  void clearSearchFilters() {
    searchedRegionList.clear();
    searchedHospitalList.clear();
    searchedPatientDoctorAppointmentHospitalsList.clear();
    searchedClinicList.clear();
    notifyListeners();
  }

  void clearSelection() {
    selectedFacilityForFilters = [];
    selectedClinicForFilters = null;
    selectedHospitalForFilters = null;
    selectedRegionForFilters = [];
    applyFilters = false;
    clearAllFilterErrors();
    notifyListeners();
  }

  void getFiltersFromDoctorList(List<DoctorsListResponseModel> doctorsList) {
    doctorsList.forEach((element) {
      if (!searchedRegionList
          .contains(element.getRegionName(appState.isArabic()))) {
        searchedRegionList
            .add(element.getRegionName(appState.isArabic()) ?? "");
      }
      if (!searchedHospitalList.contains(element.projectName)) {
        searchedPatientDoctorAppointmentHospitalsList
            .add(PatientDoctorAppointmentList()
          ..filterName = element.projectName
          ..isHMC = element.isHMC
          ..distanceInKMs = "0");
        searchedHospitalList.add(element.projectName ?? "");
      }
      if (!searchedClinicList.contains(element.clinicName)) {
        searchedClinicList.add(element.clinicName ?? "");
      }
    });
  }

  void updateApplyFilters(bool applyFilters) {
    this.applyFilters = applyFilters;
    notifyListeners();
  }

  void setSelectedRegion(String region) {
    if (selectedRegionForFilters?.contains(region) == true) {
      selectedRegionForFilters?.remove(region);
    } else {
      selectedRegionForFilters?.add(region);
    }
    notifyListeners();
  }

  void setSelectedHospital(PatientDoctorAppointmentList? hospital) {
    selectedHospitalForFilters = hospital;
    clearHospitalError();
    notifyListeners();
  }

  void setSelectedFacilityForFilter(String facility) {
    if (selectedFacilityForFilters?.contains(facility) == true) {
      selectedFacilityForFilters?.remove(facility);
    } else {
      selectedFacilityForFilters?.add(facility);
    }

    notifyListeners();
  }

  void setSelectedClinicForFilter(String? clinic) {
    selectedClinicForFilters = clinic;
    clearClinicError();
    notifyListeners();
  }

  void setSelections(
      List<String>? selectedFacilityForFilters,
      List<String>? selectedRegionForFilters,
      String? selectedClinicForFilters,
      PatientDoctorAppointmentList? selectedHospitalForFilters,
      bool applyFilters) {
    this.selectedFacilityForFilters = selectedFacilityForFilters;
    this.selectedClinicForFilters = selectedClinicForFilters;
    this.selectedHospitalForFilters = selectedHospitalForFilters;
    this.selectedRegionForFilters = selectedRegionForFilters;
    this.applyFilters = applyFilters;
    notifyListeners();
  }
}