import 'package:bloc/bloc.dart';
import 'package:dio/dio.dart';
import 'package:equatable/equatable.dart';
import 'package:hoshan/data/repository/auth_repository.dart';

part 'check_code_event.dart';
part 'check_code_state.dart';

class CheckCodeBloc extends Bloc<CheckCodeEvent, CheckCodeState> {
  CheckCodeBloc() : super(CheckCodeInitial()) {
    on<CheckCodeEvent>((event, emit) async {
      if (event is VerifyGiftCode) {
        emit(CheckCodeLoading());
        try {
          final message = await AuthRepository.checkGiftCode(event.code);
          emit(CheckCodeSucceess(message: message));
        } on DioException catch (e) {
          emit(CheckCodeFail(error: e.response!.data['detail']));
        }
      }
    });
  }
}
