import 'package:enaklo_pos/data/datasources/analytic_remote_datasource.dart';
import 'package:enaklo_pos/data/models/response/payment_method_analytic_response_model.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'payment_method_report_event.dart';
part 'payment_method_report_state.dart';
part 'payment_method_report_bloc.freezed.dart';

class PaymentMethodReportBloc
    extends Bloc<PaymentMethodReportEvent, PaymentMethodReportState> {
  final AnalyticRemoteDatasource datasource;
  PaymentMethodReportBloc(this.datasource) : super(const _Initial()) {
    on<_GetPaymentMethodReport>((event, emit) async {
      emit(const _Loading());
      final result = await datasource.getPaymentMethod(
        dateFrom: event.startDate,
        dateTo: event.endDate,
      );

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