import 'package:bloc/bloc.dart';
import 'permission_event.dart';
import 'permission_state.dart';

class PermissionBloc extends Bloc<PermissionEvent, PermissionState> {
  PermissionBloc() : super(PermissionInitial()) {
    on<LoadPermissions>(_onLoadPermissions);
    on<PermissionsReset>(_onPermissionsReset);
  }

  void _onLoadPermissions(
    LoadPermissions event,
    Emitter<PermissionState> emit,
  ) async {
    emit(PermissionLoading());
    // Simulate loading permissions from backend for orgId
    await Future.delayed(const Duration(seconds: 1));
    // Mock capabilities based on orgId
    // Allow all permissions for authenticated users (proper permissions should come from backend)
    final capabilities = Capabilities(
      canRead: true,
      canWrite: true,
      canShare: true,
      canAdmin: true,
      canAnnotate: true,
      canEdit: true,
    );
    emit(PermissionLoaded(capabilities));
  }

  void _onPermissionsReset(
    PermissionsReset event,
    Emitter<PermissionState> emit,
  ) {
    emit(PermissionInitial());
  }
}
