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

part 'category_report_event.dart';
part 'category_report_state.dart';
part 'category_report_bloc.freezed.dart';

class CategoryReportBloc
    extends Bloc<CategoryReportEvent, CategoryReportState> {
  final AnalyticRemoteDatasource _datasource;
  CategoryReportBloc(this._datasource) : super(CategoryReportState.initial()) {
    on<_Get>((event, emit) async {
      emit(_Loading());

      final result = await _datasource.getCategory(
        dateFrom: event.startDate,
        dateTo: event.endDate,
      );

      result.fold(
        (l) => emit(_Error(l)),
        (r) => emit(_Loaded(r.data!)),
      );
    });
  }
}
