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

part 'inventory_report_event.dart';
part 'inventory_report_state.dart';
part 'inventory_report_bloc.freezed.dart';

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

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

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