import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hub/features/storage/index.dart';

class ProvisionalHistoryEvent {}

class LoadProvisionalHistory extends ProvisionalHistoryEvent {}

class ProvisionalHistoryStateBloc {
  final String devUUID;
  final String userUUID;
  final String cliUUID;
  final bool isLoading;

  ProvisionalHistoryStateBloc({
    required this.devUUID,
    required this.userUUID,
    required this.cliUUID,
    this.isLoading = false,
  });

  ProvisionalHistoryStateBloc copyWith({
    String? devUUID,
    String? userUUID,
    String? cliUUID,
    bool? isLoading,
  }) {
    return ProvisionalHistoryStateBloc(
      devUUID: devUUID ?? this.devUUID,
      userUUID: userUUID ?? this.userUUID,
      cliUUID: cliUUID ?? this.cliUUID,
      isLoading: isLoading ?? this.isLoading,
    );
  }
}

class ProvisionalHistoryBloc
    extends Bloc<ProvisionalHistoryEvent, ProvisionalHistoryStateBloc> {
  ProvisionalHistoryBloc()
      : super(ProvisionalHistoryStateBloc(
            devUUID: '', userUUID: '', cliUUID: '')) {
    on<LoadProvisionalHistory>(_onLoadProvisionalHistory);
  }

  Future<void> _onLoadProvisionalHistory(
    LoadProvisionalHistory event,
    Emitter<ProvisionalHistoryStateBloc> emit,
  ) async {
    emit(state.copyWith(isLoading: true));
    final devUUID =
        (await StorageHelper().get(ProfileStorageKey.devUUID.key)) ?? '';
    final userUUID =
        (await StorageHelper().get(ProfileStorageKey.userUUID.key)) ?? '';
    final cliUUID =
        (await StorageHelper().get(ProfileStorageKey.clientUUID.key)) ?? '';
    emit(state.copyWith(
        devUUID: devUUID,
        userUUID: userUUID,
        cliUUID: cliUUID,
        isLoading: false));
  }
}
