/// BLoC pour les demandes d'aide (solidarité)
library solidarity_bloc;

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:injectable/injectable.dart';
import '../data/models/demande_aide_model.dart';
import '../data/repositories/demande_aide_repository.dart';

part 'solidarity_event.dart';
part 'solidarity_state.dart';

@injectable
class SolidarityBloc extends Bloc<SolidarityEvent, SolidarityState> {
  final DemandeAideRepository _repository;

  SolidarityBloc(this._repository) : super(const SolidarityState()) {
    on<LoadDemandesAide>(_onLoadDemandesAide);
    on<LoadDemandeAideById>(_onLoadDemandeAideById);
    on<SearchDemandesAide>(_onSearchDemandesAide);
    on<CreateDemandeAide>(_onCreateDemandeAide);
    on<ApprouverDemandeAide>(_onApprouverDemandeAide);
    on<RejeterDemandeAide>(_onRejeterDemandeAide);
  }

  Future<void> _onLoadDemandesAide(LoadDemandesAide event, Emitter<SolidarityState> emit) async {
    emit(state.copyWith(status: SolidarityStatus.loading, message: 'Chargement...'));
    try {
      final list = await _repository.getMesDemandes(page: event.page, size: event.size);
      emit(state.copyWith(status: SolidarityStatus.loaded, demandes: list));
    } catch (e) {
      emit(state.copyWith(status: SolidarityStatus.error, message: e.toString(), error: e));
    }
  }

  Future<void> _onLoadDemandeAideById(LoadDemandeAideById event, Emitter<SolidarityState> emit) async {
    emit(state.copyWith(status: SolidarityStatus.loading));
    try {
      final demande = await _repository.getById(event.id);
      emit(state.copyWith(status: SolidarityStatus.loaded, demandeDetail: demande));
    } catch (e) {
      emit(state.copyWith(status: SolidarityStatus.error, message: e.toString(), error: e));
    }
  }

  Future<void> _onSearchDemandesAide(SearchDemandesAide event, Emitter<SolidarityState> emit) async {
    emit(state.copyWith(status: SolidarityStatus.loading));
    try {
      final list = await _repository.search(
        statut: event.statut,
        type: event.type,
        page: event.page,
        size: event.size,
      );
      emit(state.copyWith(status: SolidarityStatus.loaded, demandes: list));
    } catch (e) {
      emit(state.copyWith(status: SolidarityStatus.error, message: e.toString(), error: e));
    }
  }

  Future<void> _onCreateDemandeAide(CreateDemandeAide event, Emitter<SolidarityState> emit) async {
    emit(state.copyWith(status: SolidarityStatus.loading, message: 'Création...'));
    try {
      await _repository.create(event.demande);
      add(const LoadDemandesAide());
    } catch (e) {
      emit(state.copyWith(status: SolidarityStatus.error, message: e.toString(), error: e));
    }
  }

  Future<void> _onApprouverDemandeAide(ApprouverDemandeAide event, Emitter<SolidarityState> emit) async {
    emit(state.copyWith(status: SolidarityStatus.loading));
    try {
      final updated = await _repository.approuver(event.id);
      emit(state.copyWith(status: SolidarityStatus.loaded, demandeDetail: updated));
      add(const LoadDemandesAide());
    } catch (e) {
      emit(state.copyWith(status: SolidarityStatus.error, message: e.toString(), error: e));
    }
  }

  Future<void> _onRejeterDemandeAide(RejeterDemandeAide event, Emitter<SolidarityState> emit) async {
    emit(state.copyWith(status: SolidarityStatus.loading));
    try {
      final updated = await _repository.rejeter(event.id, motif: event.motif);
      emit(state.copyWith(status: SolidarityStatus.loaded, demandeDetail: updated));
      add(const LoadDemandesAide());
    } catch (e) {
      emit(state.copyWith(status: SolidarityStatus.error, message: e.toString(), error: e));
    }
  }
}
