import 'package:bloc/bloc.dart';
import 'package:enaklo_pos/data/datasources/analytic_remote_datasource.dart';
import 'package:enaklo_pos/data/models/response/profit_loss_response_model.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'profit_loss_event.dart';
part 'profit_loss_state.dart';
part 'profit_loss_bloc.freezed.dart';

class ProfitLossBloc extends Bloc<ProfitLossEvent, ProfitLossState> {
  final AnalyticRemoteDatasource datasource;
  ProfitLossBloc(this.datasource) : super(ProfitLossState.initial()) {
    on<_GetProfitLoss>((event, emit) async {
      emit(const _Loading());
      final result = await datasource.getProfitLoss(
        dateFrom: event.startDate,
        dateTo: event.endDate,
      );
      result.fold((l) => emit(_Error(l)), (r) => emit(_Success(r.data)));
    });
  }
}
