import 'dart:typed_data'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_storage/firebase_storage.dart'; import 'package:flutter/material.dart'; import 'package:prosapp_web_app/models/pro_state.dart'; import 'package:prosapp_web_app/models/usuario.dart'; import 'package:prosapp_web_app/services/notifications_service.dart'; class ProfileFormProvider extends ChangeNotifier { Usuario? user; GlobalKey formKey = GlobalKey(); String? _verificationId; void copyUserWith({ String? id, String? email, String? phone, String? name, String? nickname, String? city, String? picture, String? birthday, String? gender, ProState? proState, String? token, }) { user = Usuario( id: id ?? user!.id, email: email ?? user!.email, phone: phone ?? user!.phone, name: name ?? user!.name, nickname: nickname ?? user!.nickname, city: city ?? user!.city, picture: picture ?? user!.picture, birthday: birthday ?? user!.birthday, gender: gender ?? user!.gender, proState: proState ?? user!.proState, token: token ?? user!.token, ); notifyListeners(); } bool _validForm() { return formKey.currentState!.validate(); } Future updateUserInfo() async { if (!_validForm()) return; final docUser = FirebaseFirestore.instance .collection('users') .doc(user!.id) .withConverter( fromFirestore: (snapshot, _) => Usuario.fromDocument(snapshot.data()!), toFirestore: (user, _) => user.toDocument(), ); await docUser.set(user!); NotificationsService.showSnackbar('Información actualizada'); } Future updateUserInfoNoValid() async { final docUser = FirebaseFirestore.instance .collection('users') .doc(user!.id) .withConverter( fromFirestore: (snapshot, _) => Usuario.fromDocument(snapshot.data()!), toFirestore: (user, _) => user.toDocument(), ); await docUser.set(user!); NotificationsService.showSnackbar('Información actualizada'); } Future uploadPicture(Uint8List bytes) async { try { final storageRef = FirebaseStorage.instance .ref() .child('${user!.id}/PP/${user!.id}_lead'); await storageRef.putData(bytes); final url = await storageRef.getDownloadURL(); copyUserWith(picture: url); notifyListeners(); return user!; } catch (e) { print("Error al subir la imagen: $e"); rethrow; } } // Agregar numero Future signUpWithPhoneNumber(String phoneNumber) async { try { await FirebaseAuth.instance.verifyPhoneNumber( phoneNumber: phoneNumber, verificationCompleted: (PhoneAuthCredential credential) async { await FirebaseAuth.instance.signInWithCredential(credential); NotificationsService.showSnackbar('Autenticación exitosa'); }, verificationFailed: (FirebaseAuthException e) { NotificationsService.showSnackbar( 'Error en la verificación: ${e.message}'); }, codeSent: (String verificationId, int? resendToken) { _verificationId = verificationId; NotificationsService.showSnackbar( 'Código enviado al número $phoneNumber'); }, codeAutoRetrievalTimeout: (String verificationId) { _verificationId = verificationId; }, ); NotificationsService.showSnackbar('Código enviado'); } catch (e) { NotificationsService.showSnackbar('Error al registrar con teléfono: $e'); } } Future linkPhoneNumberToExistingAccount( String phoneNumber, String code) async { try { var phoneAuthCredential = PhoneAuthProvider.credential( verificationId: _verificationId!, smsCode: code, ); User? user = FirebaseAuth.instance.currentUser; if (user != null) { await user.linkWithCredential(phoneAuthCredential); copyUserWith(phone: phoneNumber); notifyListeners(); NotificationsService.showSnackbar('Número vinculado exitosamente'); return true; } return false; } catch (e) { if (e is FirebaseAuthException && e.code == 'invalid-verification-code') { NotificationsService.showSnackbar('Código de verificación inválido'); return false; } else { NotificationsService.showSnackbar('Error al vincular número: $e'); rethrow; } } } }