import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow/features/auth/domain/bloc/auth_bloc.dart';

@RoutePage()
class PhoneReLoginFlowScreen extends StatelessWidget
    implements AutoRouteWrapper {
  const PhoneReLoginFlowScreen({super.key, required this.userPhone});

  final String userPhone;

  @override
  Widget wrappedRoute(BuildContext context) {
    return BlocProvider(
      create: (_) => AuthBloc()..add(AuthSendSmsToPhoneEvent(userPhone)),
      child: this,
    );
  }

  @override
  Widget build(BuildContext context) {
    return BlocListener<AuthBloc, AuthState>(
      listenWhen: (previous, current) =>
          previous.autentificated != current.autentificated,
      listener: (context, state) {
        if (state.autentificated) context.maybePop(true);
      },
      child: const AutoRouter(),
    );
  }
}
