Implement ScenarioConfigurationManager business logic
epic-scenario-based-follow-up-prompts-core-logic-task-004 — Build the ScenarioConfigurationManager service that provides chapter-level rule management. Implement loadRulesForChapter(), saveRule(rule), deleteRule(ruleId), validateRuleJson(json), and seedDefaultTemplates(chapterId). Validation uses the JSON schema from task-001. Default templates are seeded when a chapter has no rules. Depends on ScenarioRuleRepository.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
ScenarioConfigurationManager should be a plain Dart class (not a Flutter widget or BLoC) registered as a Riverpod Provider. Delegate all Supabase I/O to ScenarioRuleRepository (injected via constructor); the manager must contain zero direct Supabase calls. validateRuleJson should use the JSON schema model built in task-001 — do not re-implement schema logic here. For seedDefaultTemplates idempotency, use a Supabase upsert with an ON CONFLICT DO NOTHING strategy keyed on (chapter_id, template_name) rather than a read-then-write pattern, which is prone to race conditions.
Default templates should be defined as a Dart const list in a separate ScenarioDefaultTemplates constants file so they can be updated without modifying business logic. Keep all error types in a dedicated exceptions.dart file inside the scenario feature module.
Testing Requirements
Write unit tests using flutter_test and a mocked ScenarioRuleRepository (using Mockito or manual test doubles). Test cases must cover: (1) loadRulesForChapter returns empty list and triggers seeding, (2) loadRulesForChapter returns existing rules without re-seeding, (3) saveRule succeeds with valid JSON, (4) saveRule throws ScenarioValidationException for invalid JSON, (5) deleteRule returns true for existing ID and false for missing ID, (6) validateRuleJson passes all valid schema permutations from task-001 fixture set, (7) validateRuleJson fails with specific error messages for missing required fields, wrong types, and out-of-range values, (8) seedDefaultTemplates is idempotent under concurrent calls. Aim for 90%+ line coverage on the manager class. Integration tests against a Supabase test project should cover the RLS enforcement path.
The Rule Engine must support a flexible JSON rule schema that can express compound conditions (e.g., contact_type AND wellbeing_flag AND delay_days). Underestimating schema expressiveness may require breaking changes to the rule format after coordinators have already configured rules.
Mitigation & Contingency
Mitigation: Define and freeze the rule JSON schema (trigger_type enum, metadata_conditions structure, delay logic) before any implementation begins; validate schema against all known HLF scenarios documented in the feature spec.
Contingency: If schema changes are needed after deployment, implement a schema version field and a migration utility that upgrades stored rules to the new format without coordinator intervention.
Deep-link navigation to the activity wizard with pre-filled arguments may fail if the user's session has expired or if the wizard route is not yet mounted in the navigator stack, causing unhandled navigation exceptions.
Mitigation & Contingency
Mitigation: Implement session state check before navigation; if session is expired, redirect to biometric/login screen and store the pending deep-link URI for post-auth redirect using go_router's redirect mechanism.
Contingency: If post-auth redirect proves unreliable, fall back to navigating to the home screen with a visible action banner that re-triggers the wizard with pre-filled arguments.