import 'package:flutter/material.dart';
import 'package:prosapp_web_app/models/usuario_profesional.dart';
import 'package:prosapp_web_app/services/api_service.dart';

class ProfessionalsProvider extends ChangeNotifier {
  List<UsuarioProfesional> professionals = [];
  bool isLoading = true;
  String? _city;
  final _api = ApiService.instance;

  void updateCity(String? city) {
    if (_city != city) {
      _city = city;
      getProfessionals();
    }
  }

  getProfessionals() async {
    isLoading = true;
    notifyListeners();
    try {
      final path = (_city != null && _city!.isNotEmpty)
          ? '/professionals?city=${Uri.encodeComponent(_city!)}'
          : '/professionals';
      final res = await _api.get(path);
      final list = (res is Map ? res['data'] : res) as List;
      professionals = list
          .map((e) => UsuarioProfesional.fromDocument(e as Map<String, dynamic>))
          .toList();
    } catch (e) {
      print('Error obteniendo profesionales: $e');
    } finally {
      isLoading = false;
      notifyListeners();
    }
  }
}
