Load and manage milestone threshold definitions
epic-annual-impact-summary-core-services-task-007 — Implement the configuration loading layer for MilestoneDefinition records. Definitions should be stored as a JSON asset bundled with the app (e.g., milestones.json) listing thresholds for total hours (10h, 50h, 100h, 500h), contacts helped (1st, 10th, 100th), and streak achievements. Implement a MilestoneConfigRepository that parses this asset and exposes an ordered list of definitions sorted by significance_rank descending.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Use rootBundle.loadString('assets/milestones.json') inside an async factory. Wrap json.decode in a try/catch and rethrow as a typed MilestoneConfigException with the raw error message for debuggability. Use jsonDecode + List.from + map to MilestoneDefinition.fromJson. Sort with list.sort((a, b) => b.significanceRank.compareTo(a.significanceRank)) after parsing.
Register the repository via get_it or Riverpod Provider — do NOT use a global variable. Keep milestones.json human-readable with comments stripped (Dart's json.decode does not support JSON5). Define threshold_type as a Dart enum with a fromJson factory using a switch-case so unknown values throw early rather than silently defaulting.
Testing Requirements
Unit tests using flutter_test: (1) parse valid milestones.json fixture and assert correct MilestoneDefinition count and field values; (2) assert list is sorted by significance_rank descending; (3) parse JSON missing required fields and assert MilestoneConfigException is thrown; (4) call loadAll() twice and assert identical list reference (cache hit). No integration tests required for this task. Coverage target: 100% of MilestoneConfigRepository public methods.
Activity records may contain duplicate entries (as evidenced by the duplicate-detection feature dependency) or proxy-registered activities that should be attributed differently. Including duplicates or mis-attributed records would produce inflated stats, undermining trust in the summary.
Mitigation & Contingency
Mitigation: Implement the aggregation query to join against the deduplication-reviewed-flag on activity records and filter out unresolved duplicates. Coordinate with the duplicate-detection feature team to confirm the authoritative flag field before implementing the RPC. Include a data-quality warning in the summary when unresolved duplicates are detected.
Contingency: If deduplication state is unreliable at release time, add a prominent disclaimer in the summary UI noting that figures reflect all registered activities and may include duplicates pending review. Track a follow-up task to re-aggregate after deduplication runs.
Each organisation wants to define their own milestone thresholds (e.g., NHF's counting model differs from HLF's certification model). Implementing configurable thresholds may expand scope significantly if the configuration UI is expected in this epic.
Mitigation & Contingency
Mitigation: Scope this epic strictly to the evaluation engine and a hardcoded default threshold set. Define the MilestoneDefinition interface with an organisation_id discriminator so per-org configs can be loaded from the database in a later sprint. Build the admin configuration UI as a separate follow-on task outside this epic.
Contingency: If stakeholders require per-org milestone configuration before launch, deliver a JSON-based configuration file per org as an interim solution, loaded from Supabase storage, until a full admin UI is built.
Android 13+ restricts access to media collections and requires READ_MEDIA_IMAGES permission for gallery saves, while older Android versions use WRITE_EXTERNAL_STORAGE. Handling both permission models correctly across the device matrix is error-prone.
Mitigation & Contingency
Mitigation: Use the permission_handler Flutter package with version-aware permission requests abstracted behind the summary-share-service interface. Write platform-specific unit tests for both Android API levels in the test harness. Test on a minimum of three Android versions (API 29, 32, 34) in CI.
Contingency: If gallery save is broken on specific Android versions at launch, disable the 'Save to gallery' option on affected API levels and surface only clipboard and system share sheet, which require no media permissions.