import 'package:chain_client_sdk/chain_client_sdk.dart' as chain; import 'hub_endpoint.dart'; import 'models.dart'; import 'repository.dart'; /// Live `EvaluationRepository` backed by a Ch∆In hub over gRPC. /// /// Phase 0 scope: open a HubClient, run a healthy() probe, and /// surface the connection state to the UI. The list() / byEli() /// methods return empty data because no flow on the hub yet /// produces Evaluation objects in the lawheatmap-specific shape — /// that pipeline lands in Phase 0 week 1 once a chain run wraps /// flows/durchstich-gewo-14.yaml output into the Evaluation /// schema. /// /// Until then [MockRepository] stays the default. Pick this /// repository explicitly via the Hub-settings page; on first /// healthy() failure the app falls back to MockRepository so the /// UI keeps working. class HubRepository implements EvaluationRepository { HubRepository._(this._client, this.settings, this._healthy); final chain.HubClient _client; final HubSettings settings; final bool _healthy; /// Try to connect. Returns a repository with `isHealthy=false` /// if the probe times out or the hub is unreachable; the /// caller decides whether to use it or fall back to /// MockRepository. static Future connect(HubSettings settings) async { final endpoint = chain.HubEndpoint( host: settings.host, port: settings.port, secure: settings.secure, ); final client = chain.HubClient( endpoint: endpoint, authToken: settings.authToken, ); final ok = await client.healthy().timeout( const Duration(seconds: 4), onTimeout: () => false, ); return HubRepository._(client, settings, ok); } bool get isHealthy => _healthy; /// Underlying SDK client — exposed so the hub-status page can /// list capabilities, query the audit log, and verify the /// event-chain without leaking gRPC types to other pages. chain.HubClient get client => _client; @override Future> list() async { // Phase 0 placeholder. The pipeline that turns // flow.completed events into Evaluation objects lives in the // surrounding flow (flows/durchstich-gewo-14.yaml) and lands // in Phase 0 week 1. return const []; } @override Future byEli(String eli) async => null; @override String get mode => _healthy ? 'hub-live' : 'hub-down'; @override String get hubLabel => '${settings.url}' '${_healthy ? '' : ' — Verbindung gestört'}'; }