import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:get_it/get_it.dart';
import '../models/provider_model.dart';
import '../data/provider_repository.dart';

part 'provider_list_state.dart';

class ProviderListCubit extends Cubit<ProviderListState> {
  final ProviderRepository _repository = GetIt.I.get<ProviderRepository>();

  ProviderListCubit() : super(const ProviderListState());

  Future<void> loadProviders(String storeId) async {
    emit(state.copyWith(status: ProviderListStatus.loading));
    try {
      final providers = await _repository.getProvidersByStore(storeId);
      emit(
        state.copyWith(
          status: ProviderListStatus.success,
          providers: providers,
        ),
      );
    } catch (e) {
      emit(
        state.copyWith(
          status: ProviderListStatus.failure,
          errorMessage: e.toString(),
        ),
      );
    }
  }

  Future<void> loadAllProviders() async {
    emit(state.copyWith(status: ProviderListStatus.loading));
    try {
      final allProviders = await _repository.getAllCompanyProviders();
      emit(
        state.copyWith(
          status: ProviderListStatus.success,
          allProviders: allProviders,
        ),
      );
    } catch (e) {
      emit(
        state.copyWith(
          status: ProviderListStatus.failure,
          errorMessage: e.toString(),
        ),
      );
    }
  }
}
