	if err := r.db.WithContext(c).Select("circle_id").First(&chore, history.ChoreID).Error; err != nil {
		return fmt.Errorf("failed to get chore for history creation: %w", err)
	}

	// Allocate sync version for this history
	nextVersion, err := r.nextSyncVersion(c, chore.CircleID)
	if err != nil {
		return fmt.Errorf("failed to allocate sync version: %w", err)
	}

	history.SyncVersion = nextVersion
	return r.db.WithContext(c).Create(history).Error
}

func (r *ChoreRepository) UpdateChoreHistory(c context.Context, history *chModel.ChoreHistory) error {
	// Get the circle_id from the associated chore
	var chore chModel.Chore
	if err := r.db.WithContext(c).Select("circle_id").First(&chore, history.ChoreID).Error; err != nil {
		return fmt.Errorf("failed to get chore for history update: %w", err)
	}

	// Every history update must get a fresh sync version so it is visible to sync clients.
	nextVersion, err := r.nextSyncVersion(c, chore.CircleID)
	if err != nil {
		return fmt.Errorf("failed to allocate sync version: %w", err)
	}
	history.SyncVersion = nextVersion

	return r.db.WithContext(c).Save(history).Error
}

func (r *ChoreRepository) UpdateLatestChoreHistory(c context.Context, choreID int, updates map[string]interface{}) error {
	var chore chModel.Chore
	if err := r.db.WithContext(c).Select("circle_id").First(&chore, choreID).Error; err != nil {
		return fmt.Errorf("failed to get chore for latest history update: %w", err)
	}
	nextVersion, err := r.nextSyncVersion(c, chore.CircleID)
	if err != nil {
		return fmt.Errorf("failed to allocate sync version: %w", err)
	}
	updates["sync_version"] = nextVersion
