grantMasteryOverride method

Future<MasteryOverrideModel> grantMasteryOverride({
  1. required String topicId,
  2. required String previousMastery,
  3. required String grantedMastery,
  4. String? reason,
})

Grant a mastery override (self-assessment). Inserts the override record and updates user_topic_progress in one go.

Implementation

Future<MasteryOverrideModel> grantMasteryOverride({
  required String topicId,
  required String previousMastery,
  required String grantedMastery,
  String? reason,
}) async {
  final userId = _client.auth.currentUser?.id;
  if (userId == null) {
    throw StateError('Cannot grant override: user is not authenticated');
  }

  final response = await _client
      .from('mastery_overrides')
      .insert({
        'user_id': userId,
        'topic_id': topicId,
        'previous_mastery': previousMastery,
        'granted_mastery': grantedMastery,
        'granted_by': userId,
        'reason': reason,
      })
      .select()
      .single();

  // Also update user_topic_progress to reflect the new mastery level.
  await updateProgress(topicId, {
    'mastery': grantedMastery,
    'last_studied_at': DateTime.now().toIso8601String(),
  });

  // Fire-and-forget: check if any certifications should be auto-issued
  _tryIssueCertificates(topicId);

  return MasteryOverrideModel.fromJson(response);
}