import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:equatable/equatable.dart'; import 'package:barber_app/features/auth/data/models/user_model.dart'; import 'package:barber_app/features/auth/data/repositories/auth_repository.dart'; // Events abstract class AuthEvent extends Equatable { const AuthEvent(); @override List get props => []; } class AuthCheckRequested extends AuthEvent {} class AuthLoginRequested extends AuthEvent { final String email; final String password; const AuthLoginRequested({required this.email, required this.password}); @override List get props => [email, password]; } class AuthRegisterRequested extends AuthEvent { final String email; final String password; final String barberName; final String barberShopName; const AuthRegisterRequested({ required this.email, required this.password, required this.barberName, required this.barberShopName, }); @override List get props => [email, password, barberName, barberShopName]; } class AuthLogoutRequested extends AuthEvent {} // States abstract class AuthState extends Equatable { const AuthState(); @override List get props => []; } class AuthInitial extends AuthState {} class AuthLoading extends AuthState {} class AuthAuthenticated extends AuthState { final UserModel user; const AuthAuthenticated(this.user); @override List get props => [user]; } class AuthUnauthenticated extends AuthState {} class AuthError extends AuthState { final String message; const AuthError(this.message); @override List get props => [message]; } // Bloc class AuthBloc extends Bloc { final AuthRepository _authRepository; AuthBloc({required AuthRepository authRepository}) : _authRepository = authRepository, super(AuthInitial()) { on(_onCheckRequested); on(_onLoginRequested); on(_onRegisterRequested); on(_onLogoutRequested); } Future _onCheckRequested( AuthCheckRequested event, Emitter emit, ) async { emit(AuthLoading()); try { final user = _authRepository.getCurrentUser(); if (user != null) { emit(AuthAuthenticated(user)); } else { emit(AuthUnauthenticated()); } } catch (e) { emit(AuthUnauthenticated()); } } Future _onLoginRequested( AuthLoginRequested event, Emitter emit, ) async { emit(AuthLoading()); try { final user = await _authRepository.login( email: event.email, password: event.password, ); if (user != null) { emit(AuthAuthenticated(user)); } else { emit(const AuthError('Erro ao fazer login')); } } catch (e) { emit(AuthError(e.toString().replaceAll('Exception: ', ''))); } } Future _onRegisterRequested( AuthRegisterRequested event, Emitter emit, ) async { emit(AuthLoading()); try { final user = await _authRepository.register( email: event.email, password: event.password, barberName: event.barberName, barberShopName: event.barberShopName, ); if (user != null) { emit(AuthAuthenticated(user)); } else { emit(const AuthError('Erro ao criar conta')); } } catch (e) { emit(AuthError(e.toString().replaceAll('Exception: ', ''))); } } Future _onLogoutRequested( AuthLogoutRequested event, Emitter emit, ) async { await _authRepository.logout(); emit(AuthUnauthenticated()); } }