# Story 5.2: Event Bus System

## Metadata
- **Story ID**: 5.2
- **Title**: Event Bus System
- **Epic**: 5 - Infrastructure Adapters
- **Status**: Pending
- **Priority**: High
- **Estimated Time**: 6-8 hours
- **Dependencies**: 1.1

## Goal
Implement a complete event bus system supporting both in-process (for development/testing) and Kafka (for production) with publish/subscribe capabilities.

## Description
This story implements an event bus that allows modules to publish and subscribe to events. It supports both in-process channels for development and Kafka for production, with a clean interface that makes the implementation swappable.

## Deliverables

### 1. Event Bus Interface (`pkg/eventbus/eventbus.go`)
- `EventBus` interface with:
  - `Publish(ctx context.Context, topic string, event Event) error`
  - `Subscribe(topic string, handler EventHandler) error`
  - `Unsubscribe(topic string) error`
- `Event` and `EventHandler` types

### 2. In-Process Bus (`internal/infra/bus/inprocess_bus.go`)
- Channel-based in-process bus
- Used for testing and development
- Thread-safe implementation

### 3. Kafka Bus (`internal/infra/bus/kafka_bus.go`)
- Kafka producer for publishing
- Consumer groups for subscribing
- Error handling and retries
- Connection management

### 4. Core Events
- Define core platform events:
  - `platform.user.created`
  - `platform.user.updated`
  - `platform.role.assigned`
  - `platform.permission.granted`

### 5. Configuration
- Kafka config in `config/default.yaml`
- Bus selection (in-process vs Kafka)

### 6. DI Integration
- Provider function for EventBus
- Register in DI container
- Switchable via config

## Acceptance Criteria
- [ ] Event bus interface is defined
- [ ] In-process bus works for development
- [ ] Kafka bus works for production
- [ ] Events can be published and subscribed
- [ ] Bus is swappable via config
- [ ] Error handling works correctly
- [ ] Core events are defined

## Files to Create/Modify
- `pkg/eventbus/eventbus.go` - Event bus interface
- `internal/infra/bus/inprocess_bus.go` - In-process implementation
- `internal/infra/bus/kafka_bus.go` - Kafka implementation
- `internal/di/providers.go` - Add event bus provider
- `config/default.yaml` - Add Kafka config

