import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:prosappco/src/screens/professional.dart';

class UserModel {
  final String name;
  final String city;
  final String? profession;
  final String? state;
  final Reference? photo;
  final int? tarifa;
  final String? phoneNumber;
  final String? token;

  UserModel(this.name, this.city, this.profession, this.state, this.photo,
      this.tarifa, this.phoneNumber, this.token);

  static Future<UserModel> fromJson(
      Map<String, dynamic>? json, String uid) async {
    try {
      if (json == null) return UserModel('', '', '', null, null, 0, '', '');

      String? avatar = json['photo'];
      return UserModel(
        json['name'],
        json['city'],
        json['profesion'],
        json['estado'],
        avatar != null ? storage.ref().child(avatar) : null,
        json['tarifa'] ?? 0,
        json['phoneNumber'],
        json['token'],
      );
    } catch (e) {
      print('$e');
      return UserModel('', '', '', null, null, 0, '', '');
    }
  }

  static Future<UserModel> getUser(String uid) async {
    try {
      final snapshot =
          await FirebaseFirestore.instance.collection('users').doc(uid).get();
      final Map<String, dynamic>? data = snapshot.data();
      return fromJson(data, uid);
    } catch (e) {
      print('Error getting user: $e');
      return UserModel('', '', '', null, null, 0, '', '');
    }
  }
}
