/// Wrapper pour la page des organisations avec BLoC Provider
library organisations_page_wrapper;

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
import '../../../authentication/data/models/user_role.dart';
import '../../../authentication/presentation/bloc/auth_bloc.dart';
import '../../bloc/organizations_bloc.dart';
import '../../bloc/organizations_event.dart';
import 'organizations_page.dart';

final _getIt = GetIt.instance;

/// Wrapper qui fournit le BLoC pour la page des organisations
class OrganizationsPageWrapper extends StatelessWidget {
  const OrganizationsPageWrapper({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider<OrganizationsBloc>(
      create: (context) {
        final bloc = _getIt<OrganizationsBloc>();
        // Admin d'organisation : ne charger que son/ses organisation(s)
        final authState = context.read<AuthBloc>().state;
        final useMesOnly = authState is AuthAuthenticated &&
            authState.effectiveRole == UserRole.orgAdmin;
        bloc.add(LoadOrganizations(useMesOnly: useMesOnly));
        return bloc;
      },
      child: const OrganizationsPage(),
    );
  }
}
