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
    final capabilities = Capabilities(
      canRead: true,
      canWrite: event.orgId == 'org1', // Only admin for personal
      canShare: event.orgId == 'org1',
      canAdmin: event.orgId == 'org1',
      canAnnotate: true,
      canEdit: true,
    );
    emit(PermissionLoaded(capabilities));
  }

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