Define Dart model classes for aggregation data
epic-bufdir-data-aggregation-data-layer-task-001 — Create strongly typed Dart model classes for all aggregation-related data structures: activity records, event records, contact records, metric snapshots, geographic distribution results, and participant counts. Include fromJson/toJson serialization, equality operators, and copyWith methods. These models form the contract between the query builder and the rest of the system.
Acceptance Criteria
Technical Requirements
Implementation Notes
Place all model classes under `lib/src/features/bufdir/data/models/` using one file per model class for discoverability. Define a base `BufdirModel` abstract class with `toJson()` declared abstract and a static helper `_parseDateTime(dynamic value)` for consistent UTC normalization of all date fields from Supabase. Use `@immutable` annotation from `package:meta` on every model class. For enums (e.g., activity category types), define them in a shared `lib/src/features/bufdir/data/models/enums.dart` file with extension methods for `fromString` and `toJson` string conversion.
Avoid json_serializable or freezed unless already a project dependency — hand-written serialization is easier to audit for field-name correctness against Supabase column names. Keep field names in `fromJson` as string literals matching the Supabase schema exactly, and document any discrepancies with a comment. These models are the foundational contract for the entire data layer epic — treat them as a public API: any breaking change requires updating all consumers.
Testing Requirements
Unit tests using `flutter_test` must cover: (1) `fromJson` deserialization for each model with a complete, valid JSON fixture. (2) `toJson` serialization producing the expected Map structure for each model. (3) Round-trip test: `fromJson(model.toJson()) == model` asserts true for each model type. (4) Equality tests: two instances with identical field values are equal; changing any single field produces inequality.
(5) `copyWith` tests: verify unchanged fields are preserved and the overridden field reflects the new value. (6) Null-safety boundary tests: optional fields set to null deserialize without error; required fields missing from JSON throw a typed exception. Test coverage target: 100% of model methods.
Supabase RPC functions return JSON with PostgreSQL numeric types (bigint, numeric) that do not map cleanly to Dart int/double. Silent truncation or JSON parsing errors could corrupt participant counts in the final Bufdir submission without any runtime exception.
Mitigation & Contingency
Mitigation: Define explicit Dart fromJson factories for all RPC result models with type-safe parsing and assertion checks. Add a contract test that compares raw RPC JSON output against expected Dart model values using a known seed dataset.
Contingency: If type mismatches are found in production metrics, expose a validation endpoint in BufdirMetricsRepository that re-fetches and compares raw RPC output against the persisted snapshot, flagging any discrepancies before export proceeds.
Persisted metric snapshots can become stale if additional activities are registered after the snapshot is saved but before the export is finalized. Coordinators might unknowingly export data that does not reflect the latest activity registrations.
Mitigation & Contingency
Mitigation: Store a snapshot_generated_at timestamp and a record_count_at_generation field in the snapshot. When the coordinator views cached results, compare the current activity count for the period against the snapshot value and display a 'Data updated since last aggregation — re-run?' warning if counts differ.
Contingency: Add a mandatory staleness check before the export confirmation dialog can proceed: if the snapshot is more than 24 hours old or the record count has changed, require the coordinator to re-run aggregation before the export button is enabled.