Load scenario templates from Configuration Manager
epic-scenario-based-follow-up-prompts-scheduler-and-ui-task-012 — Wire the ScenarioConfigurationScreen to load all available scenario templates from the Scenario Configuration Manager service. Display templates grouped by category with current enabled/disabled state per chapter. Use Riverpod StreamProvider to keep the list reactive to backend changes.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Use StreamProvider.autoDispose.family, String>(chapterId) to scope by chapter. The Scenario Configuration Manager service should expose a Stream
> watchTemplatesByChapter(String chapterId) method backed by Supabase's .stream() or .select().eq('chapter_id', chapterId) with Realtime enabled on the relevant table. For grouping, compute the grouped map in the provider's map operator, not in the widget's build method. Use a SliverList with a custom delegate or a package like grouped_list to render category sections efficiently.
For sticky section headers, evaluate if the existing codebase already uses a sticky header package — prefer consistency over introducing a new dependency. Anticipate that task-013 will need to call a toggle handler — define the onToggleChanged callback signature in ScenarioTemplateListItem now (bool newValue, String templateId) -> void.
Testing Requirements
Write widget and unit tests using flutter_test. Unit test 1: scenarioTemplatesProvider emits correctly grouped and sorted ScenarioTemplateViewModel list from mock repository stream. Unit test 2: Provider correctly filters templates by the chapter ID from the auth session. Unit test 3: When stream emits a new value, the grouped list updates to reflect the change.
Widget test 1: Loading skeleton renders on first empty stream emission. Widget test 2: Correct number of SectionHeaders and list items rendered per category group. Widget test 3: Error widget shown on stream error, retry re-subscribes. Widget test 4: Provider auto-disposes (stream cancelled) when screen popped.
Mock the Scenario Configuration Manager using Mockito or manual stubs — use StreamController to simulate real-time updates in tests.
If the scheduler runs concurrently (e.g., two overlapping cron invocations due to edge function retry), duplicate prompts could be dispatched before the first run's history records are committed, breaking the deduplication guarantee.
Mitigation & Contingency
Mitigation: Use a Postgres advisory lock or unique constraint on (user_id, scenario_id, activity_ref) in the prompt history table to make concurrent writes idempotent; design the scheduler to check history inside a transaction.
Contingency: If concurrency issues persist in production, add a distributed lock via Supabase Edge Function concurrency limit (max_instances=1) for the evaluation function as a hard guard.
Coordinators may find scenario configuration unclear if trigger conditions are expressed as raw JSON or technical terminology, leading to misconfiguration and irrelevant prompts being sent to peer mentors.
Mitigation & Contingency
Mitigation: Design the ScenarioConfigurationScreen to display human-readable descriptions of each template's trigger condition (e.g., 'Send 3 days after first contact if wellbeing concern was flagged') rather than raw rule properties; validate with an HLF coordinator in a design review before implementation.
Contingency: If coordinators still misconfigure rules after launch, add a preview mode that shows a simulated prompt based on a test activity before the rule is enabled.