import 'package:flutter/material.dart';
import 'package:prosapp_web_app/models/comment_entity.dart';
import 'package:prosapp_web_app/models/score_entity.dart';
import 'package:prosapp_web_app/repositories/firebase_score_repository.dart';

class ScoreProvider with ChangeNotifier {
  final FirebaseScoreRepository _firebaseScoreRepository =
      FirebaseScoreRepository();

  ReputationEntity _reputation = const ReputationEntity(
    total: 0,
    average: 0,
    totalPro: 0,
    averagePro: 0,
  );
  ReputationEntity get reputation => _reputation;

  List<CommentEntity> _comments = [];
  List<CommentEntity> get comments => _comments;

  // Método para cargar reputación dinámica según el ID
  Future<void> loadReputation(String userId) async {
    try {
      final reputation =
          await _firebaseScoreRepository.getReputationByUserId(userId);
      _reputation = reputation;
      notifyListeners();
    } catch (e) {
      debugPrint('Error loading reputation: $e');
    }
  }

  // Método para cargar comentarios dinámicamente
  void loadComments(String userId, {required bool isProfessional}) {
    final commentStream = isProfessional
        ? _firebaseScoreRepository.getScoresForProfessional(userId)
        : _firebaseScoreRepository.getScoresForUser(userId);

    commentStream.listen((comments) {
      _comments = comments;
      notifyListeners();
    });
  }

  Future<void> addComment(CommentEntity comment) async {
    try {
      await _firebaseScoreRepository.addComment(comment);
      notifyListeners();
    } catch (e) {
      debugPrint('Error adding comment: $e');
    }
  }
}
