import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:prosappco/src/models/user_model.dart';

class ChatModel {
  List<MessageModel> messages;
  String professional_id;
  String user_id;
  UserModel? user;
  UserModel? professional;
  String id;

  ChatModel(
      {required this.messages,
      required this.professional_id,
      required this.user_id,
      this.user,
      this.professional,
      required this.id});

  static Future<ChatModel> fromDocumentSnapshot2(
      DocumentSnapshot<Map<String, dynamic>> snapshot,
      bool fillUserModel) async {
    try {
      List<MessageModel> messages = [];
      List<dynamic> messagesData = snapshot.get('message') ?? [];

      for (var data in messagesData) {
        messages.add(MessageModel(
          user: data['user'] ?? '',
          content: data['content'] ?? '',
          timestamp: (data['timestamp'] ?? '' as Timestamp).toDate(),
        ));
      }

      return ChatModel(
          messages: messages,
          professional_id: snapshot.get('professional_id') ?? '',
          user_id: snapshot.get('user_id') ?? '',
          user: fillUserModel
              ? await UserModel.getUser(snapshot.get('user_id') ?? '')
              : null,
          professional: fillUserModel
              ? await UserModel.getUser(snapshot.get('professional_id') ?? '')
              : null,
          id: snapshot.id);
    } catch (e) {
      print('error $e');
      return ChatModel(messages: [], professional_id: '', user_id: '', id: '');
    }
  }

  static ChatModel fromDocumentSnapshot(
    DocumentSnapshot<Map<String, dynamic>> snapshot,
  ) {
    try {
      List<MessageModel> messages = [];
      List<dynamic> messagesData = snapshot.get('message') ?? [];

      for (var data in messagesData) {
        messages.add(MessageModel(
          user: data['user'] ?? '',
          content: data['content'] ?? '',
          timestamp: (data['timestamp'] as Timestamp).toDate(),
        ));
      }

      return ChatModel(
          messages: messages,
          professional_id: snapshot.get('professional_id'),
          user_id: snapshot.get('user_id'),
          id: snapshot.id);
    } catch (e) {
      print('error $e');
      return ChatModel(messages: [], professional_id: '', user_id: '', id: '');
    }
  }

  static Future<List<ChatModel>> getChatsByProId(String userReceived) async {
    final receivedScoresQuery = FirebaseFirestore.instance
        .collection('chats')
        .where('professional_id', isEqualTo: userReceived);

    final receivedScoresSnapshot = await receivedScoresQuery.get();

    final receivedScores = await Future.wait(receivedScoresSnapshot.docs
        .map((doc) async => await fromDocumentSnapshot2(doc, true))
        .toList());

    return receivedScores;
  }

  static Future<List<ChatModel>> getChatsByUserId(String userReceived) async {
    final receivedScoresQuery = FirebaseFirestore.instance
        .collection('chats')
        .where('user_id', isEqualTo: userReceived);

    final receivedScoresSnapshot = await receivedScoresQuery.get();

    final receivedScores = await Future.wait(receivedScoresSnapshot.docs
        .map((doc) async => await fromDocumentSnapshot2(doc, true))
        .toList());

    return receivedScores;
  }
}

class MessageModel {
  String user;
  String content;
  DateTime timestamp;

  MessageModel(
      {required this.user, required this.content, required this.timestamp});
}
