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

class ProfessionalDetailProvider extends ChangeNotifier {
  UsuarioProfesional? professional;
  bool isLoading = true;
  final _api = ApiService.instance;

  Future<void> getProfessionalById(String uid) async {
    try {
      isLoading = true;
      notifyListeners();

      final userData = await _api.get('/users/$uid');
      final user = Usuario.fromDocument(userData as Map<String, dynamic>);

      final proData = await _api.get('/professionals/$uid');
      final professionalInfo = Profesional.fromDocument(proData as Map<String, dynamic>);

      final repData = await _api.get('/comments/reputation/$uid');
      final averageScore = (repData['average'] as num?)?.toDouble() ?? 0.0;

      professional = UsuarioProfesional(
        user: user,
        professionalInfo: professionalInfo,
        averageScore: averageScore,
      );
    } catch (e) {
      print('Error obteniendo profesional: $e');
    } finally {
      isLoading = false;
      notifyListeners();
    }
  }
}
