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

part 'item_sales_report_event.dart';
part 'item_sales_report_state.dart';
part 'item_sales_report_bloc.freezed.dart';

class ItemSalesReportBloc
    extends Bloc<ItemSalesReportEvent, ItemSalesReportState> {
  final AnalyticRemoteDatasource datasource;
  ItemSalesReportBloc(this.datasource) : super(const _Initial()) {
    on<_GetItemSales>((event, emit) async {
      emit(const _Loading());
      final result = await datasource.getSales(
        dateFrom: event.startDate,
        dateTo: event.endDate,
      );
      result.fold((l) => emit(_Error(l)), (r) => emit(_Loaded(r.data!)));
    });
  }
}
